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

apply_filter: manipulating QueryBuilder directly vs. returning a Condition? #240

Closed
spackmat opened this issue Jan 30, 2017 · 5 comments
Closed

Comments

@spackmat
Copy link
Contributor

Hi,

I have to filter for the existence of related entities. An elegant way to do this is using the is empty expression of DQL. Since I don't know a way to use is empty within Expr contexts, my working code for now is:

'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
    $query = $filterQuery->getQueryBuilder();
    if ($values['value'] == 'true') {
        $query->andWhere('e.relatedEntities is not empty');
    }
    elseif ($values['value'] == 'false') {
        $query->andWhere('e.relatedEntities is empty');
    }

    return null;
},

This works, but I have a strong feeling, that manipulating the QueryBuilder directly is bad practice and I should find a way to express this using Expr-constructs instead.

So any opnions on this?

Or an idea how I can solve this with the Expressions-interface?

Greets,
spackmat

@cedric-g
Copy link
Collaborator

Hi :), since I've added the filter_condition_builder option you can simply return a condition object in the apply_filter closure:

'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
    if ($values['value'] == 'true') {
        return $filterQuery->createCondition('e.relatedEntities is not empty');
    }
    elseif ($values['value'] == 'false') {
        return $filterQuery->createCondition('e.relatedEntities is empty');
    }

    return null;
},

@spackmat
Copy link
Contributor Author

@cedric-g cool, this makes my life easier, thanks.

But for the record: Is it bad practice to manipulate the QueryBuilder directly? Why? What's the difference, if any?

@rvanlaak
Copy link
Contributor

Manipulate directly? That actually is the idea of the library right, or how do you see this as a bad practice? You can also pass it as callable / callback if I'm right.

@cedric-g
Copy link
Collaborator

It depends on the way you work with the bundle. The fact is, if you choose to add some expressions directly to the doctrine query builder in the apply_filter it will work. But these expressions won't be handled by the filter_condition_builder as the expression won't be associatied to the field but straight to the doctrine query builder.

@spackmat
Copy link
Contributor Author

@cedric-g so now if I have a filter, which has no corresponding field/property, but uses a Repository-method to add a complex andWhere() expression to the queryBuilder instead, the following would be fine?

'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
    if (empty($values['value'])) {
        return null;
    }

    $this->myInjectedRepository->addQbMyComplexFilter($filterQuery->getQueryBuilder(), $values['value']);
    return null;
},

And in the repository to be complete:

public function addQbMyComplexFilter(QueryBuilder $qb, string $myFilterValue): QueryBuilder
{
    $qb
        ->andWhere(
            // create some complex filter expressions
        )
        ->setParameter('myFIlterValue', $myFilterValue)
    ;
    return $qb;
}

This works, but I still have a feeling, that this direct manipulation could have side effects. What are the the downsides of "But these expressions won't be handled by the filter_condition_builder"?

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

4 participants