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

Closed
opened 2026-01-22 13:17:58 +01:00 by admin · 4 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:58 +01:00
admin closed this issue 2026-01-22 13:17:59 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 29, 2011):

Comment created by mridgway:

Changed resulting queries to DQL instead of SQL, since that is where the parentheses are changed.

@doctrinebot commented on GitHub (Jun 29, 2011): Comment created by mridgway: Changed resulting queries to DQL instead of SQL, since that is where the parentheses are changed.
Author
Owner

@doctrinebot commented on GitHub (Jul 3, 2011):

Comment created by @guilhermeblanco:

Fixed.

@doctrinebot commented on GitHub (Jul 3, 2011): Comment created by @guilhermeblanco: Fixed.
Author
Owner

@doctrinebot commented on GitHub (Jul 3, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jul 3, 2011): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Jul 17, 2011):

Comment created by daveyand:

I'm still getting this aswell.

I'm working with Version: 2.1.0 [ Taken from Doctrine/ORM/Version.php ]
Although The Version within DBAL is: 2.1.0-DEV [ Taken from Doctrine/DBAL/Version.php ]

@doctrinebot commented on GitHub (Jul 17, 2011): Comment created by daveyand: I'm still getting this aswell. I'm working with Version: 2.1.0 [ Taken from Doctrine/ORM/Version.php ] Although The Version within DBAL is: 2.1.0-DEV [ Taken from Doctrine/DBAL/Version.php ]
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1557