DDC-3508: andWhere('... OR...') syntax ignores a newly added field #4321

Closed
opened 2026-01-22 14:39:24 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 16, 2015).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user delphiki:

Doctrine v2.3.6

All caches cleared before reporting.

After adding a new field on an already existing entity (field "question" on a "News" entity), this field is ignored when using the addWhere('.... OR ...') syntax, while it's working using a $qb->expr()->orX() syntax:

Not working (missing "question" column in SELECT of the generated SQL, and objects hydrated with a question's value of null) :

$qb = $this->createQueryBuilder('n');

$qb
    ->where('n.state = :state')
    ->andWhere('n.startAt <= :now')
    ->andWhere('n.endAt >= :now OR n.endAt is NULL') // Ignores the question field, don't know why
    ->setParameter('state', News::ACTIVE)
    ->setParameter('now', new \DateTime())
    ->orderBy('n.createdAt', 'DESC');

This syntax works:

$qb = $this->createQueryBuilder('n');

$qb
    ->where('n.state = :state')
    ->andWhere('n.startAt <= :now')
    ->andWhere($qb->expr()->orX(
        $qb->expr()->gte('n.endAt', ':now'),
        $qb->expr()->isNull('n.endAt')
    ))
    ->setParameter('state', News::ACTIVE)
    ->setParameter('now', new \DateTime())
    ->orderBy('n.createdAt', 'DESC');

Notes:

  • If I add an extra ->addSelect('n.question'), the field shows up twice in the generated SQL.
  • If I remove the orderBy statement or the "OR n.endAt is NULL" part, the field shows up in the SQL.
  • The generated DQL of both queries is exactly the same:
SELECT n FROM [...]\Entity\News n WHERE n.state = :state AND n.startAt <= :now AND (n.endAt >= :now OR n.endAt IS NULL) ORDER BY n.createdAt DESC
Originally created by @doctrinebot on GitHub (Jan 16, 2015). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user delphiki: Doctrine v2.3.6 All caches cleared before reporting. After adding a new field on an already existing entity (field "question" on a "News" entity), this field is ignored when using the addWhere('.... OR ...') syntax, while it's working using a $qb->expr()->orX() syntax: Not working (missing "question" column in SELECT of the generated SQL, and objects hydrated with a question's value of null) : ``` $qb = $this->createQueryBuilder('n'); $qb ->where('n.state = :state') ->andWhere('n.startAt <= :now') ->andWhere('n.endAt >= :now OR n.endAt is NULL') // Ignores the question field, don't know why ->setParameter('state', News::ACTIVE) ->setParameter('now', new \DateTime()) ->orderBy('n.createdAt', 'DESC'); ``` This syntax works: ``` $qb = $this->createQueryBuilder('n'); $qb ->where('n.state = :state') ->andWhere('n.startAt <= :now') ->andWhere($qb->expr()->orX( $qb->expr()->gte('n.endAt', ':now'), $qb->expr()->isNull('n.endAt') )) ->setParameter('state', News::ACTIVE) ->setParameter('now', new \DateTime()) ->orderBy('n.createdAt', 'DESC'); ``` Notes: - If I add an extra ->addSelect('n.question'), the field shows up twice in the generated SQL. - If I remove the orderBy statement or the "OR n.endAt is NULL" part, the field shows up in the SQL. - The generated DQL of both queries is exactly the same: ``` SELECT n FROM [...]\Entity\News n WHERE n.state = :state AND n.startAt <= :now AND (n.endAt >= :now OR n.endAt IS NULL) ORDER BY n.createdAt DESC ```
admin added the Bug label 2026-01-22 14:39:24 +01:00
admin closed this issue 2026-01-22 14:39:24 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 23, 2015):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jan 23, 2015): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jan 23, 2015):

Comment created by @ocramius:

[~delphiki] why was this marked as invalid? Was it already fixed in 2.4?

@doctrinebot commented on GitHub (Jan 23, 2015): Comment created by @ocramius: [~delphiki] why was this marked as invalid? Was it already fixed in 2.4?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4321