Skip to content

Commit e6ab99f

Browse files
committed
lazychaser#217: parent_id, lft and rgt are reset when replicating a node
1 parent 3ca7de5 commit e6ab99f

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

CHANGELOG.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 4.2.7
2+
3+
* #217: parent_id, lft and rgt are reset when replicating a node
4+
15
### 4.2.5
26

37
* #208: dirty parent and bounds when making a root

src/NodeTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,4 +1199,22 @@ protected function assertSameScope(self $node)
11991199
}
12001200
}
12011201
}
1202+
1203+
/**
1204+
* @param array|null $except
1205+
*
1206+
* @return \Illuminate\Database\Eloquent\Model
1207+
*/
1208+
public function replicate(array $except = null)
1209+
{
1210+
$defaults = [
1211+
$this->getParentIdName(),
1212+
$this->getLftName(),
1213+
$this->getRgtName(),
1214+
];
1215+
1216+
$except = $except ? array_unique(array_merge($except, $defaults)) : $defaults;
1217+
1218+
return parent::replicate($except);
1219+
}
12021220
}

tests/NodeTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function assertNodeReceivesValidValues($node)
107107
/**
108108
* @param $name
109109
*
110-
* @return \Kalnoy\Nestedset\Node
110+
* @return \Category
111111
*/
112112
public function findCategory($name, $withTrashed = false)
113113
{
@@ -952,6 +952,25 @@ public function testWhereHasCountQueryForAncestors()
952952
$this->assertEquals([ 'nokia', 'samsung', 'galaxy', 'sony', 'lenovo' ], $categories);
953953
}
954954

955+
public function testReplication()
956+
{
957+
$category = $this->findCategory('nokia');
958+
$category = $category->replicate();
959+
$category->save();
960+
$category->refreshNode();
961+
962+
$this->assertNull($category->getParentId());
963+
964+
$category = $this->findCategory('nokia');
965+
$category = $category->replicate();
966+
$category->parent_id = 1;
967+
$category->save();
968+
969+
$category->refreshNode();
970+
971+
$this->assertEquals(1, $category->getParentId());
972+
}
973+
955974
}
956975

957976
function all($items)

0 commit comments

Comments
 (0)