public function testManaged()
 {
     $test = $this->dm->find(self::ARTICLE, $this->articleId);
     $this->assertInstanceOf(self::ARTICLE, $test);
     $wrapped = new MongoDocumentWrapper($test, $this->dm);
     $this->assertEquals($this->articleId, $wrapped->getIdentifier());
     $this->assertEquals('test', $wrapped->getPropertyValue('title'));
     $wrapped->setPropertyValue('title', 'changed');
     $this->assertEquals('changed', $wrapped->getPropertyValue('title'));
     $this->assertTrue($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'], $value) : null;
                         }
                         $wrapped->setPropertyValue($field, $value);
                         unset($fields[array_search($field, $fields)]);
                     }
                 }
             }
             $filled = count($fields) === 0;
         }
         /*if (count($fields)) {
               throw new \Gedmo\Exception\UnexpectedValueException('Could not fully revert the document to version: '.$version);
           }*/
     } else {
         throw new \Gedmo\Exception\UnexpectedValueException('Could not find any log entries under version: ' . $version);
     }
 }
Exemple #3
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);
     }
     */
 }