Skip to content

Commit 892b9a7

Browse files
committed
Fixed lazychaser#252: nested eagers for ancestors and descendants are now loaded
1 parent 0adbe74 commit 892b9a7

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

README.markdown

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,17 +572,17 @@ protected function getScopeAttributes()
572572
}
573573
```
574574

575-
But now in order to execute some custom query, you need to provide attributes
575+
But now, in order to execute some custom query, you need to provide attributes
576576
that are used for scoping:
577577

578578
```php
579579
MenuItem::scoped([ 'menu_id' => 5 ])->withDepth()->get(); // OK
580580
MenuItem::descendantsOf($id)->get(); // WRONG: returns nodes from other scope
581-
MenuItem::scoped([ 'menu_id' => 5 ])->fixTree();
581+
MenuItem::scoped([ 'menu_id' => 5 ])->fixTree(); // OK
582582
```
583583

584584
When requesting nodes using model instance, scopes applied automatically based
585-
on the attributes of that model. See examples:
585+
on the attributes of that model:
586586

587587
```php
588588
$node = MenuItem::findOrFail($id);
@@ -596,12 +596,13 @@ To get scoped query builder using instance:
596596
$node->newScopedQuery();
597597
```
598598

599-
Note, that scoping is not required when retrieving model by primary key
600-
(since the key is unique):
599+
#### Scoping and eager loading
600+
601+
Always use scoped query when eager loading:
601602

602603
```php
603-
$node = MenuItem::findOrFail($id); // OK
604-
$node = MenuItem::scoped([ 'menu_id' => 5 ])->findOrFail(); // OK, but redundant
604+
MenuItem::scoped([ 'menu_id' => 5])->with('descendants')->findOrFail($id); // OK
605+
MenuItem::with('descendants')->findOrFail($id); // WRONG
605606
```
606607

607608
Requirements

src/BaseRelation.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,6 @@ public function getResults()
154154
*/
155155
public function addEagerConstraints(array $models)
156156
{
157-
$model = reset($models);
158-
159-
$this->query = $model->newScopedQuery();
160-
161157
$this->query->whereNested(function (Builder $inner) use ($models) {
162158
// We will use this query in order to apply constraints to the
163159
// base query builder

tests/NodeTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,6 @@ public function testRebuildSubtree()
827827
[ 'id' => '8' ],
828828
]);
829829

830-
echo PHP_EOL.$fixed.PHP_EOL;
831-
832830
$this->assertTrue($fixed > 0);
833831
$this->assertTreeNotBroken();
834832

tests/ScopedNodeTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public function testDescendants()
9898

9999
$this->assertEquals(1, $result->count());
100100
$this->assertEquals(5, $result->first()->getKey());
101+
102+
$node = MenuItem::scoped([ 'menu_id' => 1 ])->with('descendants')->find(2);
103+
104+
$result = $node->descendants;
105+
106+
$this->assertEquals(1, $result->count());
107+
$this->assertEquals(5, $result->first()->getKey());
101108
}
102109

103110
public function testAncestors()
@@ -109,7 +116,7 @@ public function testAncestors()
109116
$this->assertEquals(1, $result->count());
110117
$this->assertEquals(2, $result->first()->getKey());
111118

112-
$node = MenuItem::with('ancestors')->find(5);
119+
$node = MenuItem::scoped([ 'menu_id' => 1 ])->with('ancestors')->find(5);
113120

114121
$result = $node->ancestors;
115122

0 commit comments

Comments
 (0)