ソースを参照

Fixed issue #481

NestedTree Repository persistAs[...]Sibling: set correct parent
cevou 12 年 前
コミット
f5420322d3

+ 11 - 0
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -7,6 +7,7 @@ use Doctrine\ORM\Query,
     Gedmo\Tree\Strategy,
     Gedmo\Tree\Strategy\ORM\Nested,
     Gedmo\Exception\InvalidArgumentException,
+    Gedmo\Exception\UnexpectedValueException,
     Doctrine\ORM\Proxy\Proxy;
 
 /**
@@ -96,6 +97,16 @@ class NestedTreeRepository extends AbstractTreeRepository
                     throw new \Gedmo\Exception\InvalidArgumentException('If "Of" is specified you must provide parent or sibling as the second argument');
                 }
                 $parentOrSibling = $args[1];
+                $wrappedParentOrSibling = new EntityWrapper($parentOrSibling, $this->_em);
+                if (strstr($method,'Sibling')) {
+                    $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;
+                    $this->_em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $parentOrSibling);
+                }
                 $wrapped->setPropertyValue($config['parent'], $parentOrSibling);
                 $position = substr($position, 0, -2);
             }

+ 24 - 12
lib/Gedmo/Tree/Strategy/ORM/Nested.php

@@ -329,23 +329,35 @@ class Nested implements Strategy
             }
             switch ($position) {
                 case self::PREV_SIBLING:
-                    $newParent = $wrappedParent->getPropertyValue($config['parent']);
-                    if (is_null($newParent) && (isset($config['root']) || $isNewNode)) {
-                        throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible");
+                    if (property_exists($node, 'sibling')) {
+                        $wrappedSibling = AbstractWrapper::wrap($node->sibling, $em);
+                        $start = $wrappedSibling->getPropertyValue($config['left']);
+                        $level++;
+                    } else {
+                        $newParent = $wrappedParent->getPropertyValue($config['parent']);
+                        if (is_null($newParent) && (isset($config['root']) || $isNewNode)) {
+                            throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible");
+                        }
+                        $wrapped->setPropertyValue($config['parent'], $newParent);
+                        $em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $node);
+                        $start = $parentLeft;
                     }
-                    $wrapped->setPropertyValue($config['parent'], $newParent);
-                    $em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $node);
-                    $start = $parentLeft;
                     break;
 
                 case self::NEXT_SIBLING:
-                    $newParent = $wrappedParent->getPropertyValue($config['parent']);
-                    if (is_null($newParent) && (isset($config['root']) || $isNewNode)) {
-                        throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible");
+                    if (property_exists($node, 'sibling')) {
+                        $wrappedSibling = AbstractWrapper::wrap($node->sibling, $em);
+                        $start = $wrappedSibling->getPropertyValue($config['right']) + 1;
+                        $level++;
+                    } else {
+                        $newParent = $wrappedParent->getPropertyValue($config['parent']);
+                        if (is_null($newParent) && (isset($config['root']) || $isNewNode)) {
+                            throw new UnexpectedValueException("Cannot persist sibling for a root node, tree operation is not possible");
+                        }
+                        $wrapped->setPropertyValue($config['parent'], $newParent);
+                        $em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $node);
+                        $start = $parentRight + 1;
                     }
-                    $wrapped->setPropertyValue($config['parent'], $newParent);
-                    $em->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $node);
-                    $start = $parentRight + 1;
                     break;
 
                 case self::LAST_CHILD: