/** * 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(); $objectId = $wrapped->getIdentifier(); $qb = $this->createQueryBuilder(); $qb->field('objectId')->equals($objectId); $qb->field('objectClass')->equals($objectMeta->name); $qb->field('version')->lte(intval($version)); $qb->sort('version', 'ASC'); $q = $qb->getQuery(); $logs = $q->execute(); if ($logs instanceof Cursor) { $logs = $logs->toArray(); } if ($logs) { $data = array(); while ($log = array_shift($logs)) { $data = array_merge($data, $log->getData()); } $this->fillDocument($document, $data, $objectMeta); } else { throw new \Gedmo\Exception\UnexpectedValueException('Count not find any log entries under version: ' . $version); } }
public function testDetachedProxy() { $test = $this->dm->getReference(self::ARTICLE, $this->articleId); $this->dm->clear(); $wrapped = new MongoDocumentWrapper($test, $this->dm); $this->assertEquals($this->articleId, $wrapped->getIdentifier()); $this->assertEquals('test', $wrapped->getPropertyValue('title')); }
/** * 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; }
/** * 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; }