DDC-1155: Where statements not being appended #1450

Closed
opened 2026-01-22 13:14:50 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (May 17, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user nbyloff:

When I try an append new where statements using $qb->expr(), only the last where statement remains, all previous ones are overwritten. Here is an example of how I encountered this issue:

$where = array('col' => 'k.text', 'val' => 'some word%');

$qb = $this->entityManager->createQueryBuilder()
        ->select('s, sc')
        ->from('Dashboard\Entity\Section', 'sc')
        ->innerJoin('sc.keyword', 'k')
        ->innerJoin('sc.site', 's')
        ->leftJoin('k.keywordCategory', 'kc')
        ->leftJoin('k.keywordSubCategory', 'ksc');

    $qb->add('where', $qb->expr()->andx(
        $qb->expr()->eq('s.competitor', $competitor),
        $qb->expr()->eq('s.ignored', $ignored),
        $qb->expr()->eq('s.id', $params['s_id']),
        $qb->expr()->eq('s.id', 'k.targetSite')
    ), true);

//This statement will remove everything created in the above "andx". If this statement is not executed, the above "andx" remains intact.
if ($where) {
    $qb->add('where', $qb->expr()->andx(
             $qb->expr()->like($where['col'], $where['val'])
    ), true);
}

$qb->addGroupBy('k.id');
$qb->addGroupBy('s.id');

$qb->setFirstResult( $params['start'] )
   ->setMaxResults( $params['limit'] );

$q = $qb->getQuery();
echo $q->getSql();

The echo output is:

SELECT s0*.id AS id0, k1_.id AS id1, k1_.name AS name2, k2_.id AS id3, k2_.name AS   name4, k3_.id AS id5, k3_.text AS text6, k3_.search_vol AS search_vol7, s4_.id AS id8, s4_.sub_domain AS sub_domain9, MIN(s0_.rank) AS sclr10, MAX(s0*.created) AS sclr11 
FROM section s0_ 
INNER JOIN keyword k3* ON s0_.k_id = k3*.id 
INNER JOIN site s4* ON s0_.s_id = s4*.id 
LEFT JOIN keyword*category k1_ ON k3_.k_cat_id = k1*.id 
LEFT JOIN keyword*sub_category k2_ ON k3_.k_subcat_id = k2*.id 
WHERE k3_.text LIKE 'some word%' 
GROUP BY k3*.id, s4*.id LIMIT 25 OFFSET 0

This does not happen when I use a statement like this:

$qb->andWhere( $qb->expr()->like($where['col'], $where['val']) );
Originally created by @doctrinebot on GitHub (May 17, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user nbyloff: When I try an append new where statements using $qb->expr(), only the last where statement remains, all previous ones are overwritten. Here is an example of how I encountered this issue: ``` $where = array('col' => 'k.text', 'val' => 'some word%'); $qb = $this->entityManager->createQueryBuilder() ->select('s, sc') ->from('Dashboard\Entity\Section', 'sc') ->innerJoin('sc.keyword', 'k') ->innerJoin('sc.site', 's') ->leftJoin('k.keywordCategory', 'kc') ->leftJoin('k.keywordSubCategory', 'ksc'); $qb->add('where', $qb->expr()->andx( $qb->expr()->eq('s.competitor', $competitor), $qb->expr()->eq('s.ignored', $ignored), $qb->expr()->eq('s.id', $params['s_id']), $qb->expr()->eq('s.id', 'k.targetSite') ), true); //This statement will remove everything created in the above "andx". If this statement is not executed, the above "andx" remains intact. if ($where) { $qb->add('where', $qb->expr()->andx( $qb->expr()->like($where['col'], $where['val']) ), true); } $qb->addGroupBy('k.id'); $qb->addGroupBy('s.id'); $qb->setFirstResult( $params['start'] ) ->setMaxResults( $params['limit'] ); $q = $qb->getQuery(); echo $q->getSql(); ``` The echo output is: ``` SELECT s0*.id AS id0, k1_.id AS id1, k1_.name AS name2, k2_.id AS id3, k2_.name AS name4, k3_.id AS id5, k3_.text AS text6, k3_.search_vol AS search_vol7, s4_.id AS id8, s4_.sub_domain AS sub_domain9, MIN(s0_.rank) AS sclr10, MAX(s0*.created) AS sclr11 FROM section s0_ INNER JOIN keyword k3* ON s0_.k_id = k3*.id INNER JOIN site s4* ON s0_.s_id = s4*.id LEFT JOIN keyword*category k1_ ON k3_.k_cat_id = k1*.id LEFT JOIN keyword*sub_category k2_ ON k3_.k_subcat_id = k2*.id WHERE k3_.text LIKE 'some word%' GROUP BY k3*.id, s4*.id LIMIT 25 OFFSET 0 ``` This does not happen when I use a statement like this: ``` $qb->andWhere( $qb->expr()->like($where['col'], $where['val']) ); ```
admin added the Bug label 2026-01-22 13:14:50 +01:00
admin closed this issue 2026-01-22 13:14:51 +01:00
Author
Owner

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

Comment created by @beberlei:

Formatting

@doctrinebot commented on GitHub (Jun 5, 2011): Comment created by @beberlei: Formatting
Author
Owner

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

Comment created by @beberlei:

Yes that is expected, look at how andWhere() is implemented. The wiggling with ->add() is rather cumbersome and should be avoided if possible.

@doctrinebot commented on GitHub (Jun 5, 2011): Comment created by @beberlei: Yes that is expected, look at how andWhere() is implemented. The wiggling with ->add() is rather cumbersome and should be avoided if possible.
Author
Owner

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

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jun 5, 2011): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1450