/** * Allows the following 'virtual' methods: * - persistAsFirstChild($node) * - persistAsFirstChildOf($node, $parent) * - persistAsLastChild($node) * - persistAsLastChildOf($node, $parent) * - persistAsNextSibling($node) * - persistAsNextSiblingOf($node, $sibling) * - persistAsPrevSibling($node) * - persistAsPrevSiblingOf($node, $sibling) * Inherited virtual methods: * - find* * * @see \Doctrine\ORM\EntityRepository * @throws InvalidArgumentException - If arguments are invalid * @throws BadMethodCallException - If the method called is an invalid find* or persistAs* method * or no find* either persistAs* method at all and therefore an invalid method call. * @return mixed - TreeNestedRepository if persistAs* is called */ public function __call($method, $args) { if (substr($method, 0, 9) === 'persistAs') { if (!isset($args[0])) { throw new \Gedmo\Exception\InvalidArgumentException('Node to persist must be available as first argument'); } $node = $args[0]; $meta = $this->getClassMetadata(); $config = $this->listener->getConfiguration($this->_em, $meta->name); $position = substr($method, 9); if (substr($method, -2) === 'Of') { if (!isset($args[1])) { throw new \Gedmo\Exception\InvalidArgumentException('If "Of" is specified you must provide parent or sibling as the second argument'); } $parent = $args[1]; $meta->getReflectionProperty($config['parent'])->setValue($node, $parent); $position = substr($position, 0, -2); } $oid = spl_object_hash($node); $this->listener->getStrategy($this->_em, $meta->name)->setNodePosition($oid, $position); $this->_em->persist($node); return $this; } return parent::__call($method, $args); }
/** * Allows the following 'virtual' methods: * - persistAsFirstChild($node) * - persistAsFirstChildOf($node, $parent) * - persistAsLastChild($node) * - persistAsLastChildOf($node, $parent) * - persistAsNextSibling($node) * - persistAsNextSiblingOf($node, $sibling) * - persistAsPrevSibling($node) * - persistAsPrevSiblingOf($node, $sibling) * Inherited virtual methods: * - find* * * @see \Doctrine\ORM\EntityRepository * @throws InvalidArgumentException - If arguments are invalid * @throws BadMethodCallException - If the method called is an invalid find* or persistAs* method * or no find* either persistAs* method at all and therefore an invalid method call. * @return mixed - TreeNestedRepository if persistAs* is called */ public function __call($method, $args) { if (substr($method, 0, 9) === 'persistAs') { if (!isset($args[0])) { throw new \Gedmo\Exception\InvalidArgumentException('Node to persist must be available as first argument'); } $node = $args[0]; $wrapped = new EntityWrapper($node, $this->_em); $meta = $this->getClassMetadata(); $config = $this->listener->getConfiguration($this->_em, $meta->name); $position = substr($method, 9); if (substr($method, -2) === 'Of') { if (!isset($args[1])) { throw new \Gedmo\Exception\InvalidArgumentException('If "Of" is specified you must provide parent or sibling as the second argument'); } $parentOrSibling = $args[1]; if (strstr($method, 'Sibling')) { $wrappedParentOrSibling = new EntityWrapper($parentOrSibling, $this->_em); $newParent = $wrappedParentOrSibling->getPropertyValue($config['parent']); if (is_null($newParent)) { throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible"); } $node->sibling = $parentOrSibling; $parentOrSibling = $newParent; } $wrapped->setPropertyValue($config['parent'], $parentOrSibling); $position = substr($position, 0, -2); } $wrapped->setPropertyValue($config['left'], 0); // simulate changeset $oid = spl_object_hash($node); $this->listener->getStrategy($this->_em, $meta->name)->setNodePosition($oid, $position); $this->_em->persist($node); return $this; } return parent::__call($method, $args); }