Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning QueryBuilder doesn't actually clone #620

Closed
Propaganistas opened this issue Apr 8, 2021 · 0 comments · Fixed by #621
Closed

Cloning QueryBuilder doesn't actually clone #620

Propaganistas opened this issue Apr 8, 2021 · 0 comments · Fixed by #621

Comments

@Propaganistas
Copy link
Contributor

I'm building an index overview with some filter fields and also a discriminating results filter (by means of tabs):

Schermafbeelding 2021-04-08 om 16 17 59

The mentioned count badges should take into account the actual filtered result set, but of course without one filter applied.

So I have this code:

$query = QueryBuilder::for(Workshop::class)
    ->allowedFilters([
        AllowedFilter::scope('search', 'search'),
        AllowedFilter::exact('category', 'category'),
    ])
    ->allowedSorts([
        AllowedSort::field('title', 'title_normalized'),
    ]);

$published_count = $query->clone()->published(true)->count();
$unpublished_count = $query->clone()->published(false)->count();

$workshops = $query->clone()
    ->allowedFilters([
        AllowedFilter::scope('published', 'published'),
    ])
    ->paginate();

I'm first applying the other filters. Then clone the QueryBuilder to get the counts and final result set.

It seems that cloning the query builder doesn't work properly.
The internal reference to the underlying Builder is retained and hence the clone() call is a noop.

When dumping the SQL statement of $query between the counts and the final pagination call it seems that those count scopes are both incorporated, even though the $query variable was cloned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant