Skip to content

Commit

Permalink
Accurately set the lft and rght fields as you go
Browse files Browse the repository at this point in the history
Needs similar updates to moveUp
  • Loading branch information
AD7six committed Feb 8, 2015
1 parent 93d2be7 commit d3afee6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/ORM/Behavior/TreeBehavior.php
Expand Up @@ -651,6 +651,8 @@ protected function _moveDown($node, $number)
}

$edge = $this->_getMax();
$width = $node->{$right} - $node->{$left};

while ($number-- > 0) {
list($nodeLeft, $nodeRight) = array_values($node->extract([$left, $right]));

Expand All @@ -671,18 +673,15 @@ protected function _moveDown($node, $number)
$this->_sync($nextNode->{$left} - $nodeLeft, '-', "BETWEEN {$nextNode->{$left}} AND {$nextNode->{$right}}");
$this->_sync($edge - $nodeLeft - ($nextNode->{$right} - $nextNode->{$left}), '-', "> {$edge}");

$newLeft = $edge + 1;
if ($newLeft >= $nextNode->{$left} || $newLeft <= $nextNode->{$right}) {
$newLeft -= $nextNode->{$left} - $nodeLeft;
}
$newLeft -= $nextNode->{$right} - $nextNode->{$left} - 1;
$move = $nextNode->{$right} - $node->{$right};

$node->set($left, $newLeft);
$node->set($right, $newLeft + ($nodeRight - $nodeLeft));
$node->set($right, $node->{$left} + $width + $move);
$node->set($left, $node->{$left} + $move);
}

$node->dirty($left, false);
$node->dirty($right, false);

return $node;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/ORM/Behavior/TreeBehaviorTest.php
Expand Up @@ -440,6 +440,18 @@ public function testMoveDownNoTreeColumns()
$this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
}

public function testMoveDownMultiplePositions()
{
$node = $this->table->moveDown($this->table->get(3), 2);
$result = $this->table
->find('children', ['for' => 2, 'direct' => true])
->order(['lft' => 'ASC'])
->extract('id')
->toArray();

$this->assertEquals([4,5,3], $result);
}

/**
* Tests the recover function
*
Expand Down

0 comments on commit d3afee6

Please sign in to comment.