diff --git a/src/Entity.php b/src/Entity.php index c86cfc9..51fa2d2 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -522,18 +522,6 @@ public function addChildren(array $children, ?int $from = null): static return $this; } - /** - * Appends the given entity to the children relation. - * - * @internal - */ - public function appendChild(self $entity): static - { - $this->getChildrenRelation()->add($entity); - - return $this; - } - private function getChildrenRelation(): EntityCollection { if (!$this->relationLoaded(static::CHILDREN_RELATION_NAME)) { diff --git a/src/EntityCollection.php b/src/EntityCollection.php index ed0f736..5ee9ef7 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -137,7 +137,13 @@ protected function makeTree(array $items): array $parentId = $item->parent_id; if (array_key_exists($parentId, $result)) { - $result[$parentId]->appendChild($item); + $parent = $result[$parentId]; + $children = $parent->relationLoaded(Entity::CHILDREN_RELATION_NAME) + ? $parent->getRelation(Entity::CHILDREN_RELATION_NAME) + : new EntityCollection(); + + $children->add($item); + $parent->setRelation(Entity::CHILDREN_RELATION_NAME, $children); } else { $tops[] = $item; }