/** * @param ResourceControllerEvent $event */ public function onEvent(ResourceControllerEvent $event) { $document = $event->getSubject(); $metadata = $this->documentManager->getClassMetadata(get_class($document)); if ($metadata->idGenerator !== ClassMetadata::GENERATOR_TYPE_PARENT) { throw new \RuntimeException(sprintf('Document of class "%s" must be using the GENERATOR_TYPE_PARENT identificatio strategy (value %s), it is current using "%s" (this may be an automatic configuration: be sure to map both the `nodename` and the `parentDocument`).', get_class($document), ClassMetadata::GENERATOR_TYPE_PARENT, $metadata->idGenerator)); } // NOTE: that the PHPCR-ODM requires these two fields to be set when // when the GENERATOR_TYPE_PARENT "ID" strategy is used. $nameField = $metadata->nodename; $parentField = $metadata->parentMapping; $parentDocument = $metadata->getFieldValue($document, $parentField); $phpcrNode = $this->documentManager->getNodeForDocument($parentDocument); $parentPath = $phpcrNode->getPath(); $baseCandidateName = $metadata->getFieldValue($document, $nameField); $candidateName = $baseCandidateName; $index = 1; while (true) { $candidatePath = sprintf('%s/%s', $parentPath, $candidateName); $existing = $this->documentManager->find(null, $candidatePath); // if the existing document is the document we are updating, then thats great. if ($existing === $document) { return; } if (null === $existing) { $metadata->setFieldValue($document, $nameField, $candidateName); return; } $candidateName = sprintf('%s-%d', $baseCandidateName, $index); $index++; } }
/** * Just make one field of a document untranslated again */ public function testPartialUntranslateChild() { $article = new ChildTranslationArticle(); $article->id = '/functional/convert'; $article->topic = 'Some interesting subject'; $article->setText('Lorem ipsum...'); $this->dm->persist($article); $this->dm->flush(); $this->dm->clear(); $class = 'Doctrine\\Tests\\Models\\Translation\\ChildTranslationArticle'; $field = 'author'; $node = $this->node->getNode('convert'); $node->getNode('phpcr_locale:en')->setProperty($field, 'Move to untranslated'); $this->session->save(); $this->assertFalse($this->converter->convert($class, array('en'), array($field))); $this->session->save(); $this->dm->clear(); $this->dm = $this->createDocumentManager(); $this->dm->setLocaleChooserStrategy(new LocaleChooser($this->localePrefs, 'en')); $this->assertTrue($node->hasProperty($field), 'new property was not created'); $this->assertTrue($node->hasNode('phpcr_locale:en'), 'lost translation'); $this->assertFalse($node->getNode('phpcr_locale:en')->hasProperty($field), 'old property was not removed'); $article = $this->dm->find(null, '/functional/convert'); $this->assertInstanceof($class, $article); $this->assertEquals('Move to untranslated', $article->author); $this->assertEquals('Lorem ipsum...', $article->getText()); $this->dm->clear(); $article = $this->dm->find(null, '/functional/convert'); $this->assertInstanceof($class, $article); $this->assertEquals('Move to untranslated', $article->author); $this->assertEquals('Lorem ipsum...', $article->getText()); }
/** * Restore the document to the state it was before * * @param string $documentVersion the version name to restore * @param boolean $removeExisting how to handle identifier collisions * * @see VersionManager::restore */ public function restoreVersion($documentVersion, $removeExisting) { $oid = spl_object_hash($documentVersion); $history = $this->documentHistory[$oid]; $version = $this->documentVersion[$oid]; $document = $this->dm->find(null, $history->getVersionableIdentifier()); $vm = $this->session->getWorkspace()->getVersionManager(); $vm->restore($removeExisting, $version); $this->dm->refresh($document); }
function it_should_auto_increment_the_name_if_a_conflict_exists(DocumentManagerInterface $documentManager, ResourceControllerEvent $event, ClassMetadata $metadata, NodeInterface $node) { $document = new \stdClass(); $parentDocument = new \stdClass(); $existingDocument = new \stdClass(); $event->getSubject()->willReturn($document); $documentManager->getClassMetadata('stdClass')->willReturn($metadata); $metadata->idGenerator = ClassMetadata::GENERATOR_TYPE_PARENT; $metadata->nodename = 'title'; $metadata->parentMapping = 'parent'; $metadata->getFieldValue($document, 'parent')->willReturn($parentDocument); $documentManager->getNodeForDocument($parentDocument)->willReturn($node); $node->getPath()->willReturn('/path/to'); $metadata->getFieldValue($document, 'title')->willReturn('Hello World'); $documentManager->find(null, '/path/to/Hello World')->willReturn($existingDocument); $documentManager->find(null, '/path/to/Hello World-1')->willReturn($existingDocument); $documentManager->find(null, '/path/to/Hello World-2')->willReturn($existingDocument); $documentManager->find(null, '/path/to/Hello World-3')->willReturn(null); $metadata->setFieldValue($document, 'title', 'Hello World-3')->shouldBeCalled(); $this->onEvent($event); }
private function resolveParent($document, ClassMetadata $metadata) { if (!($parentField = $metadata->parentMapping)) { throw new \RuntimeException(sprintf('A default parent path has been specified, but no parent mapping has been applied to document "%s"', get_class($document))); } if (false === $this->force) { $actualParent = $metadata->getFieldValue($document, $parentField); if ($actualParent) { return; } } $parentDocument = $this->documentManager->find(null, $this->parentPath); if (true === $this->autocreate && null === $parentDocument) { NodeHelper::createPath($this->documentManager->getPhpcrSession(), $this->parentPath); $parentDocument = $this->documentManager->find(null, $this->parentPath); } if (null === $parentDocument) { throw new \RuntimeException(sprintf('Document at default parent path "%s" does not exist. `autocreate` was set to "%s"', $this->parentPath, $this->autocreate ? 'true' : 'false')); } $metadata->setFieldValue($document, $parentField, $parentDocument); }
function it_should_return_early_if_force_is_false_and_subject_already_has_a_parent(ResourceControllerEvent $event, ClassMetadata $documentMetadata, DocumentManagerInterface $documentManager) { $subjectDocument = new \stdClass(); $event->getSubject()->willReturn($subjectDocument); $documentManager->getClassMetadata(\stdClass::class)->willReturn($documentMetadata); $documentMetadata->parentMapping = 'parent'; $documentMetadata->getFieldValue($subjectDocument, 'parent')->willReturn(new \stdClass()); $documentManager->find(null, '/path/to')->shouldNotBeCalled(); $this->onPreCreate($event); }
/** * {@inheritDoc} */ public function find($className, $id) { return $this->wrapped->find($className, $id); }
/** * Find a single document by its id * * The id may either be a PHPCR path or UUID * * @param string $id document id * * @return object document or null */ public function find($id) { return $this->dm->find($this->className, $id); }