/** * Adds the entity as a root node * * Decorates via getNode() as needed * * @param mixed $entity * @return DoctrineExtensions\Hierarchical\Node */ public function addRoot($entity) { $node = $this->getNode($entity); $entity = $node->unwrap(); if ($node->getId()) { throw new HierarchicalException('This entity is already initialized and can not be made a root node'); } $this->em->getConnection()->beginTransaction(); try { $lastRoot = $this->getLastRootNode(); if ($lastRoot && $this->getNodeOrderBy()) { return $lastRoot->addSibling('sorted-sibling', $this); } if ($lastRoot) { $newPath = PathHelper::incPath($this->prototype, $lastRoot->getPath()); } else { $newPath = PathHelper::getPath($this->prototype, null, 1, 1); } $node->setValue($this->getDepthFieldName(), 1); $node->setValue($this->getPathFieldName(), $newPath); $this->em->persist($entity); $this->em->flush(); $this->em->getConnection()->commit(); } catch (\Exception $e) { $this->em->getConnection()->rollback(); $this->em->close(); throw $e; } return $node; }
/** * Returns the path for the next sibling of a given node path * * @param string $path * @return string */ protected function _incPath($path) { return PathHelper::incPath($this, $path); }