From 19152427b56ea757a483bbe84b83e65db1c4221f Mon Sep 17 00:00:00 2001 From: Ichito Nagata Date: Fri, 26 Aug 2022 21:27:59 +0900 Subject: [PATCH 1/4] unbind relations only when the entity is not recently created --- src/Models/ClosureTable.php | 6 ++++-- src/Models/Entity.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Models/ClosureTable.php b/src/Models/ClosureTable.php index e48d854..0438dfb 100644 --- a/src/Models/ClosureTable.php +++ b/src/Models/ClosureTable.php @@ -85,7 +85,7 @@ private function selectRowsToInsert($ancestorId, $descendantId) * @param mixed $ancestorId * @return void */ - public function moveNodeTo($ancestorId = null) + public function moveNodeTo($ancestorId = null, $wasRecentlyCreated = false) { $table = $this->getPrefixedTable(); $ancestor = $this->getAncestorColumn(); @@ -97,7 +97,9 @@ public function moveNodeTo($ancestorId = null) return; } - $this->unbindRelationships(); + if (! $wasRecentlyCreated) { + $this->unbindRelationships(); + } // Since we have already unbound the node relationships, // given null ancestor id, we have nothing else to do, diff --git a/src/Models/Entity.php b/src/Models/Entity.php index b5d04de..7262a06 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -318,7 +318,7 @@ public static function boot() } if ($parentIdChanged) { - $entity->closure->moveNodeTo($entity->parent_id); + $entity->closure->moveNodeTo($entity->parent_id, $entity->wasRecentlyCreated); } }); } From 2d63252f96bfe8149114985870e784bf2359d3e2 Mon Sep 17 00:00:00 2001 From: Ichito Nagata Date: Fri, 26 Aug 2022 21:57:08 +0900 Subject: [PATCH 2/4] use dedicated `$created` property instead of abusing `$wasRecentlyCreated` --- src/Models/ClosureTable.php | 4 ++-- src/Models/Entity.php | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Models/ClosureTable.php b/src/Models/ClosureTable.php index 0438dfb..522abf2 100644 --- a/src/Models/ClosureTable.php +++ b/src/Models/ClosureTable.php @@ -85,7 +85,7 @@ private function selectRowsToInsert($ancestorId, $descendantId) * @param mixed $ancestorId * @return void */ - public function moveNodeTo($ancestorId = null, $wasRecentlyCreated = false) + public function moveNodeTo($ancestorId = null, $created = false) { $table = $this->getPrefixedTable(); $ancestor = $this->getAncestorColumn(); @@ -97,7 +97,7 @@ public function moveNodeTo($ancestorId = null, $wasRecentlyCreated = false) return; } - if (! $wasRecentlyCreated) { + if (! $created) { $this->unbindRelationships(); } diff --git a/src/Models/Entity.php b/src/Models/Entity.php index 7262a06..1600bd8 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -94,6 +94,13 @@ class Entity extends Eloquent implements EntityInterface */ private $isMoved = false; + /** + * Whether this node is newly created. + * + * @var bool + */ + private $created = false; + /** * Indicates if the model should soft delete. * @@ -289,6 +296,7 @@ public static function boot() } elseif (!$entity->exists) { $entity->position = static::getLatestPosition($entity); } + $entity->created = false; }); // When entity is created, the appropriate @@ -301,6 +309,7 @@ public static function boot() $ancestor = $entity->parent_id ?? $descendant; $entity->closure->insertNode($ancestor, $descendant); + $entity->created = true; }); static::saved(static function (Entity $entity) { @@ -318,7 +327,7 @@ public static function boot() } if ($parentIdChanged) { - $entity->closure->moveNodeTo($entity->parent_id, $entity->wasRecentlyCreated); + $entity->closure->moveNodeTo($entity->parent_id, $entity->created); } }); } From 9150e4b78200f567bee3ca4d7a9322eb8352da7a Mon Sep 17 00:00:00 2001 From: Ichito Nagata Date: Sat, 27 Aug 2022 13:48:50 +0900 Subject: [PATCH 3/4] or stop calling `moveToNode` would be better --- src/Models/ClosureTable.php | 6 ++---- src/Models/Entity.php | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Models/ClosureTable.php b/src/Models/ClosureTable.php index 522abf2..e48d854 100644 --- a/src/Models/ClosureTable.php +++ b/src/Models/ClosureTable.php @@ -85,7 +85,7 @@ private function selectRowsToInsert($ancestorId, $descendantId) * @param mixed $ancestorId * @return void */ - public function moveNodeTo($ancestorId = null, $created = false) + public function moveNodeTo($ancestorId = null) { $table = $this->getPrefixedTable(); $ancestor = $this->getAncestorColumn(); @@ -97,9 +97,7 @@ public function moveNodeTo($ancestorId = null, $created = false) return; } - if (! $created) { - $this->unbindRelationships(); - } + $this->unbindRelationships(); // Since we have already unbound the node relationships, // given null ancestor id, we have nothing else to do, diff --git a/src/Models/Entity.php b/src/Models/Entity.php index 1600bd8..d105bc1 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -313,7 +313,7 @@ public static function boot() }); static::saved(static function (Entity $entity) { - $parentIdChanged = $entity->isDirty($entity->getParentIdColumn()); + $parentIdChanged = !$created && $entity->isDirty($entity->getParentIdColumn()); if ($parentIdChanged || $entity->isDirty($entity->getPositionColumn())) { $entity->reorderSiblings(); @@ -327,7 +327,7 @@ public static function boot() } if ($parentIdChanged) { - $entity->closure->moveNodeTo($entity->parent_id, $entity->created); + $entity->closure->moveNodeTo($entity->parent_id); } }); } From 96953b7d1bf7473bc55794c35949e7fe138453cd Mon Sep 17 00:00:00 2001 From: Ichito Nagata Date: Thu, 1 Sep 2022 02:59:44 +0900 Subject: [PATCH 4/4] fix --- src/Models/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Entity.php b/src/Models/Entity.php index d105bc1..d5fc997 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -313,7 +313,7 @@ public static function boot() }); static::saved(static function (Entity $entity) { - $parentIdChanged = !$created && $entity->isDirty($entity->getParentIdColumn()); + $parentIdChanged = !$entity->created && $entity->isDirty($entity->getParentIdColumn()); if ($parentIdChanged || $entity->isDirty($entity->getPositionColumn())) { $entity->reorderSiblings();