Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/NestedSetsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,17 @@ public function afterInsert(): void
*/
public function afterUpdate(): void
{
$nodeLeftValue = $this->node?->getAttribute($this->leftAttribute) ?? 0;
$nodeRightValue = $this->node?->getAttribute($this->rightAttribute) ?? 0;

match ($this->operation) {
self::OPERATION_APPEND_TO => $this->moveNode($nodeRightValue, 1),
self::OPERATION_INSERT_AFTER => $this->moveNode($nodeRightValue + 1, 0),
self::OPERATION_INSERT_BEFORE => $this->moveNode($nodeLeftValue, 0),
self::OPERATION_MAKE_ROOT => $this->moveNodeAsRoot(),
self::OPERATION_PREPEND_TO => $this->moveNode($nodeLeftValue + 1, 1),
match (true) {
$this->operation === self::OPERATION_APPEND_TO && $this->node !== null =>
$this->moveNode($this->node, $this->node->getAttribute($this->rightAttribute), 1),
$this->operation === self::OPERATION_INSERT_AFTER && $this->node !== null =>
$this->moveNode($this->node, $this->node->getAttribute($this->rightAttribute) + 1, 0),
$this->operation === self::OPERATION_INSERT_BEFORE && $this->node !== null =>
$this->moveNode($this->node, $this->node->getAttribute($this->leftAttribute), 0),
$this->operation === self::OPERATION_MAKE_ROOT =>
$this->moveNodeAsRoot(),
$this->operation === self::OPERATION_PREPEND_TO && $this->node !== null =>
$this->moveNode($this->node, $this->node->getAttribute($this->leftAttribute) + 1, 1),
default => null,
};

Expand Down Expand Up @@ -1150,23 +1152,24 @@ protected function deleteWithChildrenInternal(): bool|int
* This method is called internally during node movement operations such as append, prepend, insert before/after,
* and supports both root and non-root node moves.
*
* @param ActiveRecord $node Node to be moved within the nested set tree.
* @param int $value Left attribute value indicating the new position for the node.
* @param int $depth Depth offset to apply to the node and its descendants after the move.
*/
protected function moveNode(int $value, int $depth): void
protected function moveNode(ActiveRecord $node, int $value, int $depth): void
{
$db = $this->getOwner()::getDb();
$leftValue = $this->getOwner()->getAttribute($this->leftAttribute);
$rightValue = $this->getOwner()->getAttribute($this->rightAttribute);
$depthValue = $this->getOwner()->getAttribute($this->depthAttribute);
$depthAttribute = $db->quoteColumnName($this->depthAttribute);
$nodeDepthValue = $this->node?->getAttribute($this->depthAttribute) ?? 0;
$nodeDepthValue = $node->getAttribute($this->depthAttribute);

$depth = $nodeDepthValue - $depthValue + $depth;

if (
$this->treeAttribute === false ||
$this->getOwner()->getAttribute($this->treeAttribute) === $this->node?->getAttribute($this->treeAttribute)
$this->getOwner()->getAttribute($this->treeAttribute) === $node->getAttribute($this->treeAttribute)
) {
$delta = $rightValue - $leftValue + 1;

Expand Down Expand Up @@ -1227,7 +1230,7 @@ protected function moveNode(int $value, int $depth): void
} else {
$leftAttribute = $db->quoteColumnName($this->leftAttribute);
$rightAttribute = $db->quoteColumnName($this->rightAttribute);
$nodeRootValue = $this->node?->getAttribute($this->treeAttribute);
$nodeRootValue = $node->getAttribute($this->treeAttribute);

foreach ([$this->leftAttribute, $this->rightAttribute] as $attribute) {
$this->getOwner()::updateAll(
Expand Down