Skip to content

Commit

Permalink
Fix nested relationship manager when building the columns
Browse files Browse the repository at this point in the history
See #27
  • Loading branch information
lorisleiva committed Sep 17, 2020
1 parent a3d28f7 commit f1b1a8c
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Visitors/BuildColumnsVisitor.php
Original file line number Diff line number Diff line change
@@ -72,22 +72,25 @@ public function visitList(ListSymbol $list)
return $list;
}

protected function getNestedCallback(Collection $expressions, string $newBoolean = 'and')
protected function getNestedCallback(Collection $expressions, string $newBoolean = 'and', $newManager = null)
{
return function ($nestedBuilder) use ($expressions, $newBoolean) {
return function ($nestedBuilder) use ($expressions, $newBoolean, $newManager) {

// Save and update the new builder and boolean.
$originalBuilder = $this->builder;
$originalBoolean = $this->boolean;
$originalManager = $this->manager;
$this->builder = $nestedBuilder;
$this->boolean = $newBoolean;
$this->manager = $newManager ?: $originalManager;

// Recursively generate the nested builder.
$expressions->each->accept($this);

// Restore the original builder and boolean.
$this->builder = $originalBuilder;
$this->boolean = $originalBoolean;
$this->manager = $originalManager;

};
}
@@ -99,7 +102,9 @@ protected function buildRelationship(RelationshipSymbol $relationship)
return;
}

$callback = $this->getNestedCallback(collect([$relationship->expression]));
$nestedExpressions = collect([$relationship->expression]);
$newManager = $rule->relationshipModel->getSearchStringManager();
$callback = $this->getNestedCallback($nestedExpressions, 'and', $newManager);
$callback = $relationship->expression instanceof EmptySymbol ? null : $callback;
list($operator, $count) = $relationship->getNormalizedExpectedOperation();

2 changes: 1 addition & 1 deletion tests/CreateBuilderTest.php
Original file line number Diff line number Diff line change
@@ -196,7 +196,7 @@ public function successWhereOnly()
['comments: (not author)', "exists (select * from comments where products.id = comments.product_id and not exists (select * from users where comments.user_id = users.id))"],
['comments: (author.name: John or favourites > 5)', "exists (select * from comments where products.id = comments.product_id and (exists (select * from users where comments.user_id = users.id and name = 'John') or (select count(*) from comment_user where comments.id = comment_user.comment_id) > 5))"],
['comments: (favourites > 10) > 3', "(select count(*) from comments where products.id = comments.product_id and (select count(*) from comment_user where comments.id = comment_user.comment_id) > 10) > 3"],
['comments: ("This is great")', "exists (select * from comments where products.id = comments.product_id and (name like '%This is great%' or description like '%This is great%'))"],
['comments: ("This is great")', "exists (select * from comments where products.id = comments.product_id and (title like '%This is great%' or body like '%This is great%'))"],
['comments: (author: (name: "John Doe" age > 18)) > 3', "(select count(*) from comments where products.id = comments.product_id and exists (select * from users where comments.user_id = users.id and (name = 'John Doe' and age > 18))) > 3"],

// Relationships & And/Or.

0 comments on commit f1b1a8c

Please sign in to comment.