/**
  * Constructor
  *
  * @param SessionInterface         $session
  * @param DocumentManagerInterface $dm
  */
 public function __construct(SessionInterface $session = null, DocumentManagerInterface $dm = null)
 {
     if (!$session && $dm) {
         $session = $dm->getPhpcrSession();
     }
     parent::__construct($session);
     $this->dm = $dm;
 }
 protected function initPhpcr(DocumentManagerInterface $documentManager)
 {
     $session = $documentManager->getPhpcrSession();
     $rootNode = $session->getRootNode();
     if ($rootNode->hasNode('test')) {
         $rootNode->getNode('test')->remove();
     }
     $rootNode->addNode('test');
 }
Esempio n. 3
0
 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);
 }
Esempio n. 4
0
 /**
  * Get the state of this document
  *
  * STATE_NEW:      the document is not persisted, but a valid mapped document
  * STATE_MANAGED:  the document is tracked and will be updated on flush
  * STATE_REMOVED:  the document is scheduled for removal
  * STATE_DETACHED: there is a corresponding Node in storage, but this document is not bound to it
  *
  * @param object $document the document to get the state of
  *
  * @return int one of the STATE_* constants of this class
  */
 public function getDocumentState($document)
 {
     $oid = spl_object_hash($document);
     if (!isset($this->documentState[$oid])) {
         // this will only use the metadata if id is mapped
         $id = $this->determineDocumentId($document);
         if (!$id) {
             return self::STATE_NEW;
         }
         if ($this->getDocumentById($id)) {
             return self::STATE_DETACHED;
         }
         return $this->dm->getPhpcrSession()->nodeExists($id) ? self::STATE_DETACHED : self::STATE_NEW;
     }
     return $this->documentState[$oid];
 }
Esempio n. 5
0
 /**
  * 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);
 }
 /**
  * {@inheritDoc}
  */
 public function getPhpcrSession()
 {
     return $this->wrapped->getPhpcrSession();
 }