SonataAdminBundle: Global search ads wrong condition to custom query
My CompanyAdmin uses the createQuery method:
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$query->addSelect(
'PARTIAL ' . $query->getRootAlias() . '.{id, name, phone, fax, postcode, street, city, country}',
'PARTIAL b.{id, name}'
)
->join($query->getRootAlias() . '.branches', 'b')
;
$query->where($query->getRootAlias() . '.id = 1');
return $query;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('name', null, array(
'show_filter' => true,
'global_search' => true
))
;
}
The where condition is not added with an AND operator but an OR:
SELECT
DISTINCT c0_.id AS id_0
FROM
companies c0_
INNER JOIN branches b1_ ON c0_.id = b1_.company_id
WHERE
c0_.id = 1
OR c0_.name LIKE ?
LIMIT
10 OFFSET 0
This only happens when using the createQuery method and seems to be a problem of the global search adding it.
Without that method it is generated correctly:
SELECT
DISTINCT c0_.id AS id_0
FROM
companies c0_
WHERE
c0_.name LIKE ?
LIMIT
10 OFFSET 0
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 24 (17 by maintainers)
@greg0ire I can make PR with this changes, and may be some code refactoring
Oh sorry, you’re on the search page, right? I thought you had problems with the search handler interfering where it shouldn’t. What you expect to happen is the following, right?
WHERE existing_condition
AND(filter conditions, separated withOR)This unfortunaly works only when you use the SonataDoctrineOrmBundle.
But the methods you’re calling are not available in the ProxyQueryInterface, so that does not allow to make a general fix.
The bug is not solved. And it’s not easy to fix. Personally I would prefer to let the issue opened until a PR to fix this is merged.
Hi, any news on this ?
Thanks
Please stop 👍 on comments. Use emoji reactions for that.
👍