Skip to content

Commit da47d6d

Browse files
committed
fixed nexted sets insertion
1 parent c2d3036 commit da47d6d

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/PHPixie/ORM/Relationships/Type/NestedSet/Steps/MoveChild.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ public function execute()
7878
$this->prepareNode($child[$idField], $childLeft, $rootId, 0, $parentDepth+1);
7979
}else{
8080
//If the child was already moved to the right, we need to compensate for it
81-
$childOffset = ($rootId == $child[$rootIdKey] && $child[$leftKey] > $parent[$rightKey]) ? $width : 0;
82-
83-
$distance = $parent[$rightKey] - $child[$leftKey] - $childOffset;
81+
$childOffset = ($rootId == $child[$rootIdKey] && $child[$leftKey] > $childLeft) ? $width : 0;
8482

83+
$distance = $childLeft - $child[$leftKey] - $childOffset;
84+
8585
$this->updateQuery()
8686
->increment($leftKey, $distance)
8787
->increment($rightKey, $distance)
8888
->increment($depthKey, $parentDepth+1 - $child[$depthKey])
89-
->set($rootIdKey, $parent[$rootIdKey])
89+
->set($rootIdKey, $rootId)
9090
->where($leftKey, '>=', $child[$leftKey] + $childOffset)
9191
->where($rightKey, '<=', $child[$rightKey] + $childOffset)
9292
->where($rootIdKey, $child[$rootIdKey])

tests/PHPixie/Tests/ORM/Functional/Relationship/NestedSetTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,43 @@ protected function moveOtherTreeTest()
592592
$this->assertTree($data);
593593
}
594594

595+
public function testMoveWithoutReload()
596+
{
597+
$this->runTests('moveWithoutReload');
598+
}
599+
600+
601+
protected function moveWithoutReloadTest()
602+
{
603+
$pixie = $this->createEntity('fairy', array('name' => 'Pixie'));
604+
$trixie = $this->createEntity('fairy', array('name' => 'Trixie'));
605+
$blum = $this->createEntity('fairy', array('name' => 'Blum'));
606+
$stella = $this->createEntity('fairy', array('name' => 'Stella'));
607+
$pinky = $this->createEntity('fairy', array('name' => 'Pinky'));
608+
609+
$pinky->parent->set($stella);
610+
$stella->parent->set($blum);
611+
612+
$pixie->children->add($trixie);
613+
$pixie->children->add($blum);
614+
615+
$columns = array('id', 'name', 'left', 'right', 'depth', 'rootId');
616+
617+
$expect = array(
618+
array('1', 'Pixie', '1', '10', '0', '1'),
619+
array('2', 'Trixie', '2', '3', '1', '1'),
620+
array('3', 'Blum', '4', '9', '1', '1'),
621+
array('4', 'Stella', '5', '8', '2', '1'),
622+
array('5', 'Pinky', '6', '7', '3', '1'),
623+
);
624+
625+
foreach($expect as $key => $value) {
626+
$expect[$key] = array_combine($columns, $value);
627+
}
628+
629+
$this->assertData('fairy', $expect);
630+
}
631+
595632
public function testProperties()
596633
{
597634
$this->runTests('properties');

0 commit comments

Comments
 (0)