Skip to content

Commit 0e7eb8d

Browse files
committed
Fix node becomes dirty after update
1 parent 9b9aee1 commit 0e7eb8d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/Kalnoy/Nestedset/Node.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,14 @@ protected function actionAppendOrPrepend(Node $parent, $prepend = false)
273273

274274
$parent->refreshNode();
275275

276-
return $this->insertAt($prepend ? $parent->getLft() + 1 : $parent->getRgt());
276+
if ($this->insertAt($prepend ? $parent->getLft() + 1 : $parent->getRgt()))
277+
{
278+
$parent->refreshNode();
279+
280+
return true;
281+
}
282+
283+
return false;
277284
}
278285

279286
/**
@@ -340,6 +347,7 @@ public function refreshNode()
340347
$attributes = $this->newServiceQuery()->getNodeData($this->getKey());
341348

342349
$this->attributes = array_merge($this->attributes, $attributes);
350+
$this->original = array_merge($this->original, $attributes);
343351
}
344352

345353
/**
@@ -580,7 +588,15 @@ public function before(Node $node)
580588
*/
581589
public function insertBefore(Node $node)
582590
{
583-
return $this->before($node)->save();
591+
if ($this->before($node)->save())
592+
{
593+
// We'll' update the target node since it will be moved
594+
$node->refreshNode();
595+
596+
return true;
597+
}
598+
599+
return false;
584600
}
585601

586602
/**

tests/NodeTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ public function testRecievesValidValuesWhenAppendedTo()
119119
$node = new Category([ 'name' => 'test' ]);
120120
$root = Category::root();
121121

122+
$accepted = array($root->_rgt, $root->_rgt + 1, $root->id);
123+
122124
$root->append($node);
123125

124-
$this->assertEquals(array($root->_rgt, $root->_rgt + 1, $root->id), $this->nodeValues($node));
126+
$this->assertEquals($accepted, $this->nodeValues($node));
125127
$this->assertTreeNotBroken();
128+
$this->assertFalse($node->isDirty());
126129
}
127130

128131
public function testRecievesValidValuesWhenPrependedTo()
@@ -143,6 +146,7 @@ public function testRecievesValidValuesWhenInsertedAfter()
143146

144147
$this->assertEquals(array($target->_rgt + 1, $target->_rgt + 2, $target->parent->id), $this->nodeValues($node));
145148
$this->assertTreeNotBroken();
149+
$this->assertFalse($node->isDirty());
146150
}
147151

148152
public function testRecievesValidValuesWhenInsertedBefore()
@@ -446,6 +450,15 @@ public function testCreatesNode()
446450
$this->assertEquals(19, $node->getLft());
447451
}
448452

453+
public function testCreatesViaRelationship()
454+
{
455+
$node = $this->findCategory('apple');
456+
457+
$child = $node->children()->create([ 'name' => 'test' ]);
458+
459+
$this->assertTreeNotBroken();
460+
}
461+
449462
public function testCreatesTree()
450463
{
451464
$node = Category::create(
@@ -454,10 +467,12 @@ public function testCreatesTree()
454467
'children' =>
455468
[
456469
[ 'name' => 'test2' ],
470+
[ 'name' => 'test3' ],
457471
],
458472
]);
459473

474+
$this->assertTreeNotBroken();
460475
$this->assertTrue(isset($node->children));
461-
$this->assertCount(1, $node->children);
476+
$this->assertCount(2, $node->children);
462477
}
463478
}

0 commit comments

Comments
 (0)