/** * 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 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); }