/** * Use the name and parent fields to generate the id * * {@inheritDoc} */ public function generate($document, ClassMetadata $class, DocumentManager $dm, $parent = null) { if (null === $parent) { $parent = $class->parentMapping ? $class->getFieldValue($document, $class->parentMapping) : null; } $name = $class->nodename ? $class->getFieldValue($document, $class->nodename) : null; $id = $class->getFieldValue($document, $class->identifier); if (empty($id)) { if (empty($name) && empty($parent)) { throw IdException::noIdentificationParameters($document, $class->parentMapping, $class->nodename); } if (empty($parent)) { throw IdException::noIdNoParent($document, $class->parentMapping); } if (empty($name)) { throw IdException::noIdNoName($document, $class->nodename); } } // use assigned ID by default if (empty($parent) || empty($name)) { return $id; } if ($exception = $class->isValidNodename($name)) { throw IdException::illegalName($document, $class->nodename, $name); } // determine ID based on the path and the node name return $this->buildName($document, $class, $dm, $parent, $name); }
/** * Use the identifier field as id and throw exception if not set. * * {@inheritDoc} */ public function generate($document, ClassMetadata $cm, DocumentManagerInterface $dm, $parent = null) { $id = $cm->getFieldValue($document, $cm->identifier); if (!$id) { throw new IdException('ID could not be read from the document instance using the AssignedIdGenerator.'); } return $id; }
/** * @param object $document * @param ClassMetadata $cm * @param DocumentManager $dm * @return string */ public function generate($document, ClassMetadata $cm, DocumentManager $dm) { $id = $cm->getFieldValue($document, $cm->identifier); if (!$id) { throw new \RuntimeException('ID could not be determined. Make sure the document has a property with Doctrine\\ODM\\PHPCR\\Mapping\\Annotations\\Id annotation and that the property is set to the path where the document is to be stored.'); } return $id; }
/** * @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; }
/** * Use the parent field together with an auto generated name to generate the id * * {@inheritDoc} */ public function generate($document, ClassMetadata $class, DocumentManagerInterface $dm, $parent = null) { if (null === $parent) { $parent = $class->parentMapping ? $class->getFieldValue($document, $class->parentMapping) : null; } $id = $class->getFieldValue($document, $class->identifier); if (empty($id) && null === $parent) { throw IdException::noIdNoParent($document, $class->parentMapping); } if (empty($parent)) { return $id; } try { $parentNode = $dm->getNodeForDocument($parent); $existingNames = (array) $parentNode->getNodeNames(); } catch (RepositoryException $e) { // this typically happens while cascading persisting documents $existingNames = array(); } $name = NodeHelper::generateAutoNodeName($existingNames, $dm->getPhpcrSession()->getWorkspace()->getNamespaceRegistry()->getNamespaces(), '', ''); return $this->buildName($document, $class, $dm, $parent, $name); }
/** * Use a repository that implements RepositoryIdGenerator to generate the id. * * {@inheritDoc} */ public function generate($document, ClassMetadata $class, DocumentManagerInterface $dm, $parent = null) { if (null === $parent) { $parent = $class->parentMapping ? $class->getFieldValue($document, $class->parentMapping) : null; } $repository = $dm->getRepository($class->name); if (!$repository instanceof RepositoryIdInterface) { throw new IdException("ID could not be determined. Make sure the that the Repository '" . ClassUtils::getClass($repository) . "' implements RepositoryIdInterface"); } $id = $repository->generateId($document, $parent); if (!$id) { throw new IdException("ID could not be determined. Repository was unable to generate an ID"); } return $id; }
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); }
private function doUpdate(ClassMetadataFactory $metadataFactory, PhpcrMetadata $odmMetadata, CtMetadata $ctMetadata, $document) { $documentId = $odmMetadata->getIdentifierValue($document); foreach ($odmMetadata->childrenMappings as $childrenField) { // if the children field is not managed by the CT component, // continue if (!isset($ctMetadata->propertyMetadata[$childrenField])) { continue; } $children = $odmMetadata->getFieldValue($document, $childrenField); // note that we do not preserve array keys. PHPCR ODM will return a // children collection using the PHPCR property names as keys, so // we currently have no control over how these keys populated. $index = 0; foreach ($children as $child) { $childMetadata = $metadataFactory->getMetadataFor(ClassUtils::getRealClass(get_class($child))); $newId = sprintf('%s/%s', $documentId, $this->encoder->encode($childrenField, $index++)); $childMetadata->setIdentifierValue($child, $newId); } } }
/** * Set the mapped mixins. * * @param ClassMetadata $metadata * @param NodeInterface $node * @param object $document The document to update autogenerated fields. */ private function setMixins(Mapping\ClassMetadata $metadata, NodeInterface $node, $document) { $repository = $this->session->getRepository(); if ($metadata->versionable === 'full') { if ($repository->getDescriptor(RepositoryInterface::OPTION_VERSIONING_SUPPORTED)) { $node->addMixin('mix:versionable'); } elseif ($repository->getDescriptor(RepositoryInterface::OPTION_SIMPLE_VERSIONING_SUPPORTED)) { $node->addMixin('mix:simpleVersionable'); } } elseif ($metadata->versionable === 'simple' && $repository->getDescriptor(RepositoryInterface::OPTION_SIMPLE_VERSIONING_SUPPORTED)) { $node->addMixin('mix:simpleVersionable'); } if (!$node->isNodeType('mix:referenceable') && $metadata->referenceable) { $node->addMixin('mix:referenceable'); } // manually set the uuid if it is not present yet, so we can assign it to documents if ($node->isNodeType('mix:referenceable') && !$node->hasProperty('jcr:uuid')) { $uuid = false; $uuidFieldName = $metadata->getUuidFieldName(); if ($uuidFieldName) { $uuid = $metadata->getFieldValue($document, $uuidFieldName); } if (!$uuid) { $uuid = $this->generateUuid(); } $node->setProperty('jcr:uuid', $uuid); if ($uuidFieldName && !$metadata->getFieldValue($document, $uuidFieldName)) { $metadata->setFieldValue($document, $uuidFieldName, $uuid); } } }
/** * @param object $document The document to convert * @param TranslationStrategyInterface $previousStrategy Translation strategy to remove fields from old location * @param ClassMetadata $previousMeta Metadata for old translation strategy * @param TranslationStrategyInterface $currentStrategy Translation strategy to save new translations * @param ClassMetadata $currentMeta Metadata for new translation strategy * @param array $fields The fields to handle * @param array $locales Target locales to copy translations to. * @param bool $partialUntranslate Whether we are only a subset of fields back to untranslated */ private function convertDocument($document, TranslationStrategyInterface $previousStrategy, ClassMetadata $previousMeta, TranslationStrategyInterface $currentStrategy, ClassMetadata $currentMeta, array $fields, array $locales, $partialUntranslate) { $node = $this->dm->getNodeForDocument($document); $data = array(); foreach ($fields as $field) { $data[$field] = $currentMeta->getFieldValue($document, $field); } if ($currentStrategy instanceof NonTranslatedStrategy) { $currentStrategy->saveTranslation($data, $node, $currentMeta, null); } else { foreach ($locales as $locale) { $currentStrategy->saveTranslation($data, $node, $currentMeta, $locale); } } if ($partialUntranslate && $previousStrategy instanceof ChildTranslationStrategy) { // the child translation strategy would remove the whole child node foreach ($previousStrategy->getLocalesFor($document, $node, $previousMeta) as $locale) { $translation = $node->getNode(Translation::LOCALE_NAMESPACE . ':' . $locale); foreach ($fields as $field) { $translation->setProperty($previousMeta->mappings[$field]['property'], null); } } } else { $previousStrategy->removeAllTranslations($document, $node, $previousMeta); } }