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

Wrong search results when using custom query #2850

Closed
guillaumesmo opened this issue Mar 25, 2015 · 7 comments · Fixed by sonata-project/SonataDoctrineORMAdminBundle#1358
Closed

Comments

@guillaumesmo
Copy link

Hi,

I have overloaded the createQuery in my Admin class, but the search results include wrong items because the resulting query is invalid :

public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);
    $query->andWhere($query->getRootAlias() . '.deletedAt IS NOT NULL');
    return $query;
}

SELECT DISTINCT d0_.id AS id0 FROM data d0_ WHERE d0_.deletedAt IS NULL OR d0_.firstName LIKE '%search%' OR d0_.lastName LIKE '%search%' LIMIT 25 OFFSET 0

The search criteria should be wrapped in a AND (...), right now they are just appended as OR

Tested with sonata-project/admin-bundle 2.3.2

@soerenbernstein
Copy link

+1
Have the same exact problem.
Any work arounds for now?

@rande
Copy link
Member

rande commented Mar 26, 2015

By default the search does a OR on text field. It is not a bug, it is correct behavior to look over all fields.

@rande rande closed this as completed Mar 26, 2015
@guillaumesmo
Copy link
Author

I agree that the different criteria must be searched on with an OR, but the custom query should be respected in the first place, so the final query should look like this :

SELECT DISTINCT d0_.id AS id0 FROM data d0_ WHERE d0_.deletedAt IS NULL AND (d0_.firstName LIKE '%search%' OR d0_.lastName LIKE '%search%') LIMIT 25 OFFSET 0

@rande
Copy link
Member

rande commented Mar 26, 2015

There is nothing in the Admin Bundle which set this criteria d0_.deletedAt IS NULL in the query.

@soerenbernstein
Copy link

The problem is, that the OR filter part should be in parantheses so an earlier added AND where clause is not destroyed. I'm not sure if this is a error in the Doctrine\ORM\QueryBuilder class, but adding the and part first will not result in the intended where clause. Adding it after appliing all the OR filters will result in an where clause where all the OR filter are in parentheses.

I've fiddled around in the Datagrid class and found a rather crud solution. It's probably not the best, but seems to solve the problem with a minimum amount of changes.

@soerenbernstein
Copy link

@rande So, I updated mix fix according to your notes and created a new PR. Is there any chance this will be included?.

@brossdu
Copy link

brossdu commented Nov 10, 2015

Any news concerning this issue ?

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.

4 participants