Skip to content

Commit e342cd9

Browse files
authored
Merge pull request #232 from franzose/6.x
fixed #231 regression by utilizing relation methods as it was in ClosureTable 5
2 parents 8d946eb + 05aa525 commit e342cd9

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/Extensions/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function makeTree(array $items)
171171
$parentId = $item->parent_id;
172172

173173
if (array_key_exists($parentId, $result)) {
174-
$result[$parentId]->children->add($item);
174+
$result[$parentId]->appendChild($item);
175175
} else {
176176
$tops[] = $item;
177177
}

src/Models/Entity.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
*/
6565
class Entity extends Eloquent implements EntityInterface
6666
{
67+
const CHILDREN_RELATION_NAME = 'children';
68+
6769
/**
6870
* ClosureTable model instance.
6971
*
@@ -263,7 +265,7 @@ public function getRealDepthColumn()
263265
*/
264266
public function getChildrenRelationIndex()
265267
{
266-
return 'children';
268+
return static::CHILDREN_RELATION_NAME;
267269
}
268270

269271
/**
@@ -927,6 +929,29 @@ public function addChildren(array $children, $from = null)
927929
return $this;
928930
}
929931

932+
/**
933+
* Appends the given entity to the children relation.
934+
*
935+
* @param Entity $entity
936+
* @internal
937+
*/
938+
public function appendChild(Entity $entity)
939+
{
940+
$this->getChildrenRelation()->add($entity);
941+
}
942+
943+
/**
944+
* @return Collection
945+
*/
946+
private function getChildrenRelation()
947+
{
948+
if (!$this->relationLoaded(static::CHILDREN_RELATION_NAME)) {
949+
$this->setRelation(static::CHILDREN_RELATION_NAME, new Collection());
950+
}
951+
952+
return $this->getRelation(static::CHILDREN_RELATION_NAME);
953+
}
954+
930955
/**
931956
* Removes a model's child with given position.
932957
*
@@ -1665,7 +1690,7 @@ public static function getTree(array $columns = ['*'])
16651690
$instance = new static;
16661691

16671692
return $instance
1668-
->load('children')
1693+
->load(static::CHILDREN_RELATION_NAME)
16691694
->orderBy($instance->getParentIdColumn())
16701695
->orderBy($instance->getPositionColumn())
16711696
->get($instance->prepareTreeQueryColumns($columns))
@@ -1727,7 +1752,7 @@ public static function createFromArray(array $tree, EntityInterface $parent = nu
17271752
$entities = [];
17281753

17291754
foreach ($tree as $item) {
1730-
$children = Arr::pull($item, 'children');
1755+
$children = Arr::pull($item, static::CHILDREN_RELATION_NAME);
17311756

17321757
/**
17331758
* @var Entity $entity

tests/Extensions/CollectionTests.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Franzose\ClosureTable\Tests;
33

4+
use DB;
45
use Franzose\ClosureTable\Extensions\Collection;
56
use Franzose\ClosureTable\Models\Entity;
67

@@ -150,12 +151,19 @@ public function testHasChildren()
150151

151152
public function testToTree()
152153
{
154+
$queries = 0;
155+
156+
DB::listen(static function () use (&$queries) {
157+
$queries++;
158+
});
159+
153160
$root = new Page(['id' => 1]);
154161
$child = new Page(['id' => 2, 'parent_id' => 1]);
155162
$grandChild = new Page(['id' => 3, 'parent_id' => 2]);
156163

157164
$tree = (new Collection([$root, $child, $grandChild]))->toTree();
158165

166+
static::assertEquals(0, $queries);
159167
static::assertCount(1, $tree);
160168

161169
$children = $tree->get(0)->children;

0 commit comments

Comments
 (0)