Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Conditional Joins with shared filters #216

Closed
tkleinhakisa opened this issue Jun 15, 2016 · 3 comments
Closed

Conditional Joins with shared filters #216

tkleinhakisa opened this issue Jun 15, 2016 · 3 comments

Comments

@tkleinhakisa
Copy link

Hello,
I'm having an issue filtering an association that has a lot of data.

Disclaimer: i'm using an old version of the bundle and cannot update right now.

Here is my use case:
I have a Club entity with Users belonging to clubs
I want to be able to filter clubs depending on some user's properties: for example i have a user_id field which will filter to get all clubs containing the given user.

In order to do that i have to JOIN the users in the query, but then the query execution becomes slow if no data is given in the user filter
The join is added in the closure given to $qbe->addOnce

$closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) {
    $filterBuilder->leftJoin($alias .'.users', 'u');
};

What i would like is to join IF AND ONLY IF the user "sub-filter" has been filled and the join is truly required to make the filtering.

Do you think it is possible ?
Is it something that has been implemented in the latest version of the bundle ?

For now my only work-around was to use the $_REQUEST superglobal in my closure to check the submitted filter data

$closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) {
    if (isset($_REQUEST[...]) {
        $filterBuilder->leftJoin($alias .'.users', 'u');
    }
};

Any help or suggestion would be really appreciated
Thank you everyone for the efforts in this bundle

@cedric-g
Copy link
Collaborator

Hi, even on the latest version there is no way to add a join with this kind of condition as the $qbe->addOnce() is not aware of submitted values.

@minychillo
Copy link

I know this is an old post, but did you find a solution to this ?

@ianwilk52
Copy link
Contributor

You need to implement your own version of the filterEntity function to only make the join if data is given. Something like the following change:

-- if (is_object($values['value'])) {
++ if (is_object($values['value']) && count($values['value'])) {

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

No branches or pull requests

5 participants