Skip to content

Commit

Permalink
fixed #231 regression by utilizing relation methods as it was in Clos…
Browse files Browse the repository at this point in the history
…ureTable 5
  • Loading branch information
franzose committed May 11, 2020
1 parent 30d60da commit 05aa525
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Extensions/Collection.php
Expand Up @@ -171,7 +171,7 @@ protected function makeTree(array $items)
$parentId = $item->parent_id;

if (array_key_exists($parentId, $result)) {
$result[$parentId]->children->add($item);
$result[$parentId]->appendChild($item);
} else {
$tops[] = $item;
}
Expand Down
31 changes: 28 additions & 3 deletions src/Models/Entity.php
Expand Up @@ -64,6 +64,8 @@
*/
class Entity extends Eloquent implements EntityInterface
{
const CHILDREN_RELATION_NAME = 'children';

/**
* ClosureTable model instance.
*
Expand Down Expand Up @@ -263,7 +265,7 @@ public function getRealDepthColumn()
*/
public function getChildrenRelationIndex()
{
return 'children';
return static::CHILDREN_RELATION_NAME;
}

/**
Expand Down Expand Up @@ -927,6 +929,29 @@ public function addChildren(array $children, $from = null)
return $this;
}

/**
* Appends the given entity to the children relation.
*
* @param Entity $entity
* @internal
*/
public function appendChild(Entity $entity)
{
$this->getChildrenRelation()->add($entity);
}

/**
* @return Collection
*/
private function getChildrenRelation()
{
if (!$this->relationLoaded(static::CHILDREN_RELATION_NAME)) {
$this->setRelation(static::CHILDREN_RELATION_NAME, new Collection());
}

return $this->getRelation(static::CHILDREN_RELATION_NAME);
}

/**
* Removes a model's child with given position.
*
Expand Down Expand Up @@ -1665,7 +1690,7 @@ public static function getTree(array $columns = ['*'])
$instance = new static;

return $instance
->load('children')
->load(static::CHILDREN_RELATION_NAME)
->orderBy($instance->getParentIdColumn())
->orderBy($instance->getPositionColumn())
->get($instance->prepareTreeQueryColumns($columns))
Expand Down Expand Up @@ -1727,7 +1752,7 @@ public static function createFromArray(array $tree, EntityInterface $parent = nu
$entities = [];

foreach ($tree as $item) {
$children = Arr::pull($item, 'children');
$children = Arr::pull($item, static::CHILDREN_RELATION_NAME);

/**
* @var Entity $entity
Expand Down
8 changes: 8 additions & 0 deletions tests/Extensions/CollectionTests.php
@@ -1,6 +1,7 @@
<?php
namespace Franzose\ClosureTable\Tests;

use DB;
use Franzose\ClosureTable\Extensions\Collection;
use Franzose\ClosureTable\Models\Entity;

Expand Down Expand Up @@ -150,12 +151,19 @@ public function testHasChildren()

public function testToTree()
{
$queries = 0;

DB::listen(static function () use (&$queries) {
$queries++;
});

$root = new Page(['id' => 1]);
$child = new Page(['id' => 2, 'parent_id' => 1]);
$grandChild = new Page(['id' => 3, 'parent_id' => 2]);

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

static::assertEquals(0, $queries);
static::assertCount(1, $tree);

$children = $tree->get(0)->children;
Expand Down

0 comments on commit 05aa525

Please sign in to comment.