public function testSomeFunctions()
 {
     $test = new Article();
     $wrapped = new MongoDocumentWrapper($test, $this->dm);
     $wrapped->populate(array('title' => 'test'));
     $this->assertEquals('test', $wrapped->getPropertyValue('title'));
     $this->assertFalse($wrapped->hasValidIdentifier());
 }
    /**
     * Reverts given $document to $revision by
     * restoring all fields from that $revision.
     * After this operation you will need to
     * persist and flush the $document.
     *
     * @param object $document
     * @param integer $version
     * @throws \Gedmo\Exception\UnexpectedValueException
     * @return void
     */
    public function revert($document, $version = 1)
    {
        $wrapped = new MongoDocumentWrapper($document, $this->dm);
        $objectMeta = $wrapped->getMetadata();
        $meta = $this->getClassMetadata();
        $objectId = $wrapped->getIdentifier();

        $qb = $this->createQueryBuilder();
        $qb->field('objectId')->equals($objectId);
        $qb->field('objectClass')->equals($objectMeta->name);
        $qb->field('version')->lte($version);
        $qb->sort('version', 'ASC');
        $q = $qb->getQuery();

        $logs = $q->execute();
        if ($logs instanceof Cursor) {
            $logs = $logs->toArray();
        }
        if ($logs) {
            $config = $this->getLoggableListener()->getConfiguration($this->dm, $objectMeta->name);
            $fields = $config['versioned'];
            $filled = false;
            while (($log = array_pop($logs)) && !$filled) {
                if ($data = $log->getData()) {
                    foreach ($data as $field => $value) {
                        if (in_array($field, $fields)) {
                            if ($objectMeta->isSingleValuedAssociation($field)) {
                                $mapping = $objectMeta->getFieldMapping($field);
                                $value = $value ? $this->dm->getReference($mapping['targetDocument'], current($value)) : null;
                            }
                            $wrapped->setPropertyValue($field, $value);
                            unset($fields[array_search($field, $fields)]);
                        }
                    }
                }
                $filled = count($fields) === 0;
            }
            if (count($fields)) {
                throw new \Gedmo\Exception\UnexpectedValueException('Cound not fully revert the document to version: '.$version);
            }
        } else {
            throw new \Gedmo\Exception\UnexpectedValueException('Count not find any log entries under version: '.$version);
        }
    }
 /**
  * Loads all translations with all translatable
  * fields from the given entity
  *
  * @param object $document
  * @return array list of translations in locale groups
  */
 public function findTranslations($document)
 {
     $result = array();
     $wrapped = new MongoDocumentWrapper($document, $this->dm);
     if ($wrapped->hasValidIdentifier()) {
         $documentId = $wrapped->getIdentifier();
         $translationMeta = $this->getClassMetadata();
         $qb = $this->createQueryBuilder();
         $q = $qb->field('foreignKey')->equals($documentId)->field('objectClass')->equals($wrapped->getMetadata()->name)->sort('locale', 'asc')->getQuery();
         $q->setHydrate(false);
         $data = $q->execute();
         if ($data instanceof Cursor) {
             $data = $data->toArray();
         }
         if ($data && is_array($data) && count($data)) {
             foreach ($data as $row) {
                 $result[$row['locale']][$row['field']] = $row['content'];
             }
         }
     }
     return $result;
 }
 /**
  * Get children from node
  *
  * @return Doctrine\ODM\MongoDB\QueryBuilder
  */
 public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc')
 {
     $meta = $this->getClassMetadata();
     $config = $this->listener->getConfiguration($this->dm, $meta->name);
     $separator = preg_quote($config['path_separator']);
     $qb = $this->dm->createQueryBuilder()->find($meta->name);
     if (is_object($node) && $node instanceof $meta->name) {
         $node = new MongoDocumentWrapper($node, $this->dm);
         $nodePath = preg_quote($node->getPropertyValue($config['path']));
         if ($direct) {
             $regex = sprintf('/^%s[^%s]+%s$/', $nodePath, $separator, $separator);
         } else {
             $regex = sprintf('/^%s.+/', $nodePath);
         }
         $qb->field($config['path'])->equals(new \MongoRegex($regex));
     } else {
         if ($direct) {
             $qb->field($config['path'])->equals(new \MongoRegex(sprintf('/^[^%s]+%s$/', $separator, $separator)));
         }
     }
     $qb->sort(is_null($sortByField) ? $config['path'] : $sortByField, $direction === 'asc' ? 'asc' : 'desc');
     return $qb;
 }
Example #5
0
 /**
  * Fills a documents versioned fields with data
  *
  * @param object $document
  * @param array $data
  */
 protected function fillDocument($document, array $data)
 {
     $wrapped = new MongoDocumentWrapper($document, $this->dm);
     $objectMeta = $wrapped->getMetadata();
     $config = $this->getLoggableListener()->getConfiguration($this->dm, $objectMeta->name);
     $fields = $config['versioned'];
     foreach ($data as $field => $value) {
         if (!in_array($field, $fields)) {
             continue;
         }
         $mapping = $objectMeta->getFieldMapping($field);
         // Fill the embedded document
         if ($wrapped->isEmbeddedAssociation($field)) {
             if (!empty($value)) {
                 $embeddedMetadata = $this->dm->getClassMetadata($mapping['targetDocument']);
                 $document = $embeddedMetadata->newInstance();
                 $this->fillDocument($document, $value);
                 $value = $document;
             }
         } elseif ($objectMeta->isSingleValuedAssociation($field)) {
             $value = $value ? $this->dm->getReference($mapping['targetDocument'], $value) : null;
         }
         $wrapped->setPropertyValue($field, $value);
         unset($fields[$field]);
     }
     /*
     if (count($fields)) {
         throw new \Gedmo\Exception\UnexpectedValueException('Cound not fully revert the document to version: '.$version);
     }
     */
 }
 /**
  * Loads all translations with all translatable
  * fields from the given entity
  *
  * @param object $document
  *
  * @return array list of translations in locale groups
  */
 public function findTranslations($document)
 {
     $result = array();
     $wrapped = new MongoDocumentWrapper($document, $this->dm);
     if ($wrapped->hasValidIdentifier()) {
         $documentId = $wrapped->getIdentifier();
         $translationMeta = $this->getClassMetadata();
         // table inheritance support
         $config = $this->getTranslatableListener()->getConfiguration($this->dm, get_class($document));
         $translationClass = isset($config['translationClass']) ? $config['translationClass'] : $translationMeta->rootDocumentName;
         $qb = $this->dm->createQueryBuilder($translationClass);
         $q = $qb->field('foreignKey')->equals($documentId)->field('objectClass')->equals($wrapped->getMetadata()->rootDocumentName)->field('content')->exists(true)->notEqual(null)->sort('locale', 'asc')->getQuery();
         $q->setHydrate(false);
         $data = $q->execute();
         if ($data instanceof Cursor) {
             $data = $data->toArray();
         }
         if ($data && is_array($data) && count($data)) {
             foreach ($data as $row) {
                 $result[$row['locale']][$row['field']] = $row['content'];
             }
         }
     }
     return $result;
 }