Skip to content

Commit

Permalink
Fix populateTree() wrong secondary using of variable $depth. Close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
paulzi committed Oct 13, 2016
1 parent d8350ad commit 2d5aaae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
6 changes: 3 additions & 3 deletions NestedSetsBehavior.php
Expand Up @@ -226,8 +226,10 @@ public function populateTree($depth = null)
$nodes = $this->getDescendants($depth)->all();
}

$key = $this->owner->getAttribute($this->leftAttribute);
$relates = [];
$parents = [$this->owner->getAttribute($this->leftAttribute)];
$relates[$key] = [];
$parents = [$key];
$prev = $this->owner->getAttribute($this->depthAttribute);
foreach($nodes as $node)
{
Expand All @@ -251,8 +253,6 @@ public function populateTree($depth = null)
$key = $node->getAttribute($this->leftAttribute);
if (isset($relates[$key])) {
$node->populateRelation('children', $relates[$key]);
} elseif ($depth === null) {
$node->populateRelation('children', []);
}
}

Expand Down
67 changes: 34 additions & 33 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/NestedSetsBehaviorTestCase.php
Expand Up @@ -132,6 +132,10 @@ public function testPopulateTree()
$node = Node::findOne(19);
$node->populateTree();
$this->assertEquals(true, $node->isRelationPopulated('children'));

$node = Node::findOne(19);
$node->populateTree(1);
$this->assertEquals(true, $node->isRelationPopulated('children'));
}

public function testIsRoot()
Expand Down

0 comments on commit 2d5aaae

Please sign in to comment.