/** * Initializes a new <tt>DocumentRepository</tt>. * * @param DocumentManager $dm The DocumentManager to use. * @param ClassMetadata $classMetadata The class descriptor. */ public function __construct($dm, Mapping\ClassMetadata $class) { $this->dm = $dm; $this->class = $class; $this->uow = $this->dm->getUnitOfWork(); $this->className = $class->name; }
/** * {@inheritDoc} */ public function getContentId($content) { if (!is_object($content)) { return null; } try { return $this->documentManager->getUnitOfWork()->getDocumentId($content); } catch (\Exception $e) { return null; } }
/** * Test that children are retrieved in the parent locale */ public function testFindTranslationWithUntranslatedChildren() { $this->dm->persist($this->doc); $this->dm->bindTranslation($this->doc, 'en'); $this->doc->topic = 'Un autre sujet'; $this->dm->bindTranslation($this->doc, 'fr'); $this->dm->flush(); $testNode = $this->node->getNode($this->testNodeName); $testNode->addNode('new-comment'); $this->session->save(); $this->dm->clear(); $this->doc = $this->dm->findTranslation($this->class, '/functional/' . $this->testNodeName, 'fr'); $this->assertEquals('fr', $this->doc->locale); $children = $this->doc->getChildren(); $this->assertCount(1, $children); foreach ($children as $comment) { $this->assertInstanceOf('Doctrine\\ODM\\PHPCR\\Document\\Generic', $comment); $this->assertNull($this->dm->getUnitOfWork()->getCurrentLocale($comment)); } $this->dm->clear(); $this->doc = $this->dm->findTranslation($this->class, '/functional/' . $this->testNodeName, 'en'); $children = $this->dm->getChildren($this->doc); $this->assertCount(1, $children); foreach ($children as $comment) { $this->assertInstanceOf('Doctrine\\ODM\\PHPCR\\Document\\Generic', $comment); $this->assertNull($this->dm->getUnitOfWork()->getCurrentLocale($comment)); } }
public function testPersistRepository() { $doc = new RepositoryIdStrategy(); $doc->title = 'repository strategy'; $this->dm->persist($doc); $this->dm->flush(); $id = $this->dm->getUnitOfWork()->getDocumentId($doc); $this->dm->clear(); $this->assertInstanceOf('\\Doctrine\\Tests\\ODM\\PHPCR\\Functional\\Mapping\\RepositoryIdStrategy', $this->dm->find(null, $id)); }
protected function buildName($document, ClassMetadata $class, DocumentManager $dm, $parent, $name) { // get the id of the parent document $id = $dm->getUnitOfWork()->getDocumentId($parent); if (!$id) { throw IdException::parentIdCouldNotBeDetermined($document, $class->parentMapping, $parent); } // edge case parent is root if ('/' === $id) { $id = ''; } return $id . '/' . $name; }
/** * @param object $document * @param ClassMetadata $cm * @param DocumentManager $dm * @return string */ public function generate($document, ClassMetadata $cm, DocumentManager $dm) { $parent = $cm->getFieldValue($document, $cm->parentMapping); $name = $cm->getFieldValue($document, $cm->nodename); $id = $cm->getFieldValue($document, $cm->identifier); if ((empty($parent) || empty($name)) && empty($id)) { throw new \RuntimeException('ID could not be determined. Make sure the document has a property with Doctrine\\ODM\\PHPCR\\Mapping\\Annotations\\ParentDocument and Doctrine\\ODM\\PHPCR\\Mapping\\Annotations\\Nodename annotation and that the property is set to the path where the document is to be stored.'); } // use assigned ID by default if (!$parent || empty($name)) { return $id; } // determine ID based on the path and the node name $id = $dm->getUnitOfWork()->getDocumentId($parent); if (!$id) { throw new \RuntimeException('Parent ID could not be determined. Make sure to persist the parent document before persisting this document.'); } return $id . '/' . $name; }
private static function objToStr($obj, DocumentManager $dm = null) { $string = method_exists($obj, '__toString') ? (string) $obj : ClassUtils::getClass($obj) . '@' . spl_object_hash($obj); if ($dm) { try { $id = $dm->getUnitOfWork()->determineDocumentId($obj); if (!$id) { $id = 'unmanaged or new document without id'; } $string .= " ({$id})"; } catch (\Exception $e) { $id = 'failed to determine id'; $string .= " ({$id})"; } } return $string; }
function let(DocumentManager $dm, ClassMetadata $class, UnitOfWork $unitOfWork) { $dm->getUnitOfWork()->shouldBeCalled()->willreturn($unitOfWork); $this->beConstructedWith($dm, $class); }
public function setUp() { $this->dm = $this->createDocumentManager(); $this->uow = $this->dm->getUnitOfWork(); $this->resetFunctionalNode($this->dm); }
public function testTriggerEvents() { $this->dm->getEventManager()->addEventListener(array(Event::prePersist, Event::postPersist, Event::preUpdate, Event::postUpdate, Event::preRemove, Event::postRemove, Event::onFlush, Event::postFlush, Event::preFlush, Event::preMove, Event::postMove, Event::endFlush), $this->listener); $page = new CmsPage(); $page->title = "my-page"; $page->content = "long story"; $this->dm->persist($page); $this->assertTrue($this->listener->pagePrePersist); $this->assertFalse($this->listener->itemPrePersist); $this->assertFalse($this->listener->postFlush); $this->assertFalse($this->listener->endFlush); $this->assertFalse($this->listener->preFlush); $this->dm->flush(); $this->assertTrue($this->listener->onFlush); $this->assertTrue($this->listener->postFlush); $this->assertTrue($this->listener->endFlush); $this->assertTrue($this->listener->preFlush); $this->assertFalse($this->listener->preUpdate); $this->assertFalse($this->listener->postUpdate); $this->assertTrue($this->listener->pagePostPersist); $this->assertFalse($this->listener->itemPostPersist); $this->assertFalse($this->listener->pagePreRemove); $this->assertFalse($this->listener->pagePostRemove); $this->assertFalse($this->listener->itemPreRemove); $this->assertFalse($this->listener->pagePreMove); $this->assertFalse($this->listener->pagePostMove); $this->assertFalse($this->listener->itemPreMove); $this->assertFalse($this->listener->itemPostMove); $this->dm->move($page, '/functional/moved-' . $page->title); $this->assertFalse($this->listener->pagePreMove); $this->assertFalse($this->listener->pagePostMove); $this->dm->flush(); $this->assertTrue($this->listener->pagePreMove); $this->assertTrue($this->listener->pagePostMove); $this->assertFalse($this->listener->itemPreMove); $this->assertFalse($this->listener->itemPostMove); $this->dm->flush(); $item = new CmsItem(); $item->name = "my-item"; $item->documentTarget = $page; $page->content = "short story"; $this->dm->persist($item); $page->addItem($item); $this->dm->flush(); $this->assertTrue($this->listener->preUpdate); $this->assertTrue($this->listener->itemPrePersist); $this->assertTrue($this->listener->postUpdate); $this->assertTrue($this->listener->itemPostPersist); $this->assertEquals('long story is now short story', $page->content); $pageId = $this->dm->getUnitOfWork()->getDocumentId($page); $itemId = $this->dm->getUnitOfWork()->getDocumentId($item); $this->dm->clear(); $page = $this->dm->find(null, $pageId); $item = $this->dm->find(null, $itemId); $this->assertEquals('long story is now short story', $page->content); $this->dm->remove($item); $this->dm->remove($page); $this->assertTrue($this->listener->pagePreRemove); $this->assertTrue($this->listener->itemPreRemove); $this->assertFalse($this->listener->pagePostRemove); $this->assertFalse($this->listener->itemPostRemove); $this->assertFalse($this->dm->contains($page)); $this->assertFalse($this->dm->contains($item)); $this->dm->flush(); $this->assertFalse($this->dm->contains($page)); $this->assertFalse($this->dm->contains($item)); $this->assertTrue($this->listener->pagePostRemove); $this->assertTrue($this->listener->itemPostRemove); }