public function insertBefore(NodeInterface $movedNode, NodeInterface $newDescendant) { // 1. movedNode.parentId = newDescendant.parentId, movedNode.position = MAX(newDescendant.position,1) // 2. removeFromSort(movedNode) like delete without delete // 2. UPDATE SET position = position+1 WHERE parentId = newAncestor.parentId // AND position >= movedNode.position // 3. movedNode.save() $targetParentId = $newDescendant->__get($this->parentCol()); $targetPosition = max($newDescendant->__get($this->sortCol()), 1); $oldParentId = $movedNode->__get($this->parentCol()); $oldPosition = $movedNode->__get($this->sortCol()); // Nothing to do if ($targetParentId == $oldParentId && $targetPosition == $oldPosition && $node->exists) { return $this; } $movedNode->__set($this->parentCol(), $targetParentId); $movedNode->__set($this->sortCol(), $targetPosition); // Remove node from old parent $this->decrementOrderAfter($oldParentId, $oldPosition); // Create space for new position $this->incrementOrderAfter($targetParentId, $targetPosition - 1); $movedNode->save(); return $this; }
public function makeRootNode(Node $node) { $node->__set($this->parentCol(), NULL); $rootId = $node->__get($this->rootCol()); if (!$rootId) { $rootId = $node->__get($this->pkCol()); } if ($rootId) { $node->__set($this->rootCol(), $rootId); } $node->save(); if (!$rootId) { $rootId = $node->__get($this->pkCol()); $node->__set($this->rootCol(), $rootId); } $node->__set($this->parentCol(), NULL); return $this; }