DDC-1237: Where clause grouping changed for 2.1 #1556

Open
opened 2026-01-22 13:17:54 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 29, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user mridgway:

Running through our tests for a project with the dev releases of Doctrine 2.1 we noticed some differences in the queries that were generated using the query builder.

The thing that is breaking our queries is the where clauses are not grouped any more. Before there were parentheses wrapping each where clause and now they are not. So in our case when we have an OR statement in a where and then we add another AND, they match up incorrectly.

The query builder looks like this:

$em->getConfiguration()->addCustomStringFunction('DISTANCE', 'Geocode\Doctrine\Distance');

$qb = $em->getRepository('Core\Model\Activity\ContentActivity')->createQueryBuilder('a');
$qb->where("a INSTANCE OF Pro\Model\Activity\CompanyCreated OR a INSTANCE OF Pro\Model\Activity\EmploymentApproved OR a INSTANCE OF Ask\Model\Activity\QuestionAnswered OR a INSTANCE OF Knowhow\Model\Activity\ArticleCreated");

if($radius && $latitude && $longitude) {
    $qb->join('a.location', 'l');
    $qb->setParameter('1', $latitude);
    $qb->setParameter('2', $longitude);
    $qb->setParameter('3', $radius);
    $qb->andWhere("DISTANCE(l.latitude, l.longitude, ?1, ?2) < ?3");
}

if($isFeatured) {
    $qb->join('a.content', 'c');
    $qb->andWhere('c.isFeatured = true');
}

if ($startDate) {
    $qb->andWhere(sprintf("a.dateCreated <= '%s'", date('Y-m-d G:i:s', (int)$startDate)));
}
if ($offset) {
    $qb->setFirstResult((int)$offset);
}
$qb->setMaxResults((int)$totalResults);
$qb->orderBy('a.dateCreated', 'desc');

$results = $qb->getQuery()->getResult();

Prior to RC1 the generated DQL look like this:

SELECT a FROM Core\Model\Activity\ContentActivity a 
INNER JOIN a.location l 
INNER JOIN a.content c 
WHERE (a INSTANCE OF Pro\Model\Activity\CompanyCreated OR a INSTANCE OF Pro\Model\Activity\EmploymentApproved OR a INSTANCE OF Ask\Model\Activity\QuestionAnswered OR a INSTANCE OF Knowhow\Model\Activity\ArticleCreated) 
AND (DISTANCE(l.latitude, l.longitude, ?1, ?2) < ?3) AND (c.isFeatured = true) 
ORDER BY a.dateCreated desc

After RC1 it looks like this:

SELECT a FROM Core\Model\Activity\ContentActivity a 
INNER JOIN a.location l 
INNER JOIN a.content c 
WHERE a INSTANCE OF Pro\Model\Activity\CompanyCreated OR a INSTANCE OF Pro\Model\Activity\EmploymentApproved OR a INSTANCE OF Ask\Model\Activity\QuestionAnswered OR a INSTANCE OF Knowhow\Model\Activity\ArticleCreated 
AND DISTANCE(l.latitude, l.longitude, ?1, ?2) < ?3 AND c.isFeatured = true 
ORDER BY a.dateCreated desc
Originally created by @doctrinebot on GitHub (Jun 29, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user mridgway: Running through our tests for a project with the dev releases of Doctrine 2.1 we noticed some differences in the queries that were generated using the query builder. The thing that is breaking our queries is the where clauses are not grouped any more. Before there were parentheses wrapping each where clause and now they are not. So in our case when we have an OR statement in a where and then we add another AND, they match up incorrectly. The query builder looks like this: ``` $em->getConfiguration()->addCustomStringFunction('DISTANCE', 'Geocode\Doctrine\Distance'); $qb = $em->getRepository('Core\Model\Activity\ContentActivity')->createQueryBuilder('a'); $qb->where("a INSTANCE OF Pro\Model\Activity\CompanyCreated OR a INSTANCE OF Pro\Model\Activity\EmploymentApproved OR a INSTANCE OF Ask\Model\Activity\QuestionAnswered OR a INSTANCE OF Knowhow\Model\Activity\ArticleCreated"); if($radius && $latitude && $longitude) { $qb->join('a.location', 'l'); $qb->setParameter('1', $latitude); $qb->setParameter('2', $longitude); $qb->setParameter('3', $radius); $qb->andWhere("DISTANCE(l.latitude, l.longitude, ?1, ?2) < ?3"); } if($isFeatured) { $qb->join('a.content', 'c'); $qb->andWhere('c.isFeatured = true'); } if ($startDate) { $qb->andWhere(sprintf("a.dateCreated <= '%s'", date('Y-m-d G:i:s', (int)$startDate))); } if ($offset) { $qb->setFirstResult((int)$offset); } $qb->setMaxResults((int)$totalResults); $qb->orderBy('a.dateCreated', 'desc'); $results = $qb->getQuery()->getResult(); ``` Prior to RC1 the generated DQL look like this: ``` SELECT a FROM Core\Model\Activity\ContentActivity a INNER JOIN a.location l INNER JOIN a.content c WHERE (a INSTANCE OF Pro\Model\Activity\CompanyCreated OR a INSTANCE OF Pro\Model\Activity\EmploymentApproved OR a INSTANCE OF Ask\Model\Activity\QuestionAnswered OR a INSTANCE OF Knowhow\Model\Activity\ArticleCreated) AND (DISTANCE(l.latitude, l.longitude, ?1, ?2) < ?3) AND (c.isFeatured = true) ORDER BY a.dateCreated desc ``` After RC1 it looks like this: ``` SELECT a FROM Core\Model\Activity\ContentActivity a INNER JOIN a.location l INNER JOIN a.content c WHERE a INSTANCE OF Pro\Model\Activity\CompanyCreated OR a INSTANCE OF Pro\Model\Activity\EmploymentApproved OR a INSTANCE OF Ask\Model\Activity\QuestionAnswered OR a INSTANCE OF Knowhow\Model\Activity\ArticleCreated AND DISTANCE(l.latitude, l.longitude, ?1, ?2) < ?3 AND c.isFeatured = true ORDER BY a.dateCreated desc ```
admin added the Bug label 2026-01-22 13:17:54 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1556