Partial query ignores where statement if not in select #5717

Closed
opened 2026-01-22 15:15:23 +01:00 by admin · 6 comments
Owner

Originally created by @escopecz on GitHub (Sep 26, 2017).

Originally assigned to: @Ocramius on GitHub.

Following query examples are output of getDql() method with pasted parameters for easier understanding of the problem.

This query does what I'd expect. It returns all company fields:

SELECT f FROM Field f INDEX BY f.id WHERE f.object = 'company'

However this query returns ALL fields, not only company fields:

SELECT f.{id} FROM Field f INDEX BY f.id WHERE f.object = 'company'

It took me a while to find out what's going on there. If I specifically list the object column in the select clause, the where condition will work again and only company fields will be returned:

SELECT f.{id,object} FROM Field f INDEX BY f.id WHERE f.object = 'company'

I couldn't find any documentation on this topic nor any reported issue. Why does it work that way? Isn't it a bug?

Originally created by @escopecz on GitHub (Sep 26, 2017). Originally assigned to: @Ocramius on GitHub. Following query examples are output of `getDql()` method with pasted parameters for easier understanding of the problem. This query does what I'd expect. It returns all company fields: ``` SELECT f FROM Field f INDEX BY f.id WHERE f.object = 'company' ``` However this query returns ALL fields, not only company fields: ``` SELECT f.{id} FROM Field f INDEX BY f.id WHERE f.object = 'company' ``` It took me a while to find out what's going on there. If I specifically list the `object` column in the select clause, the where condition will work again and only company fields will be returned: ``` SELECT f.{id,object} FROM Field f INDEX BY f.id WHERE f.object = 'company' ``` I couldn't find any documentation on this topic nor any reported issue. Why does it work that way? Isn't it a bug?
admin added the InvalidQuestion labels 2026-01-22 15:15:23 +01:00
admin closed this issue 2026-01-22 15:15:25 +01:00
Author
Owner

@Ocramius commented on GitHub (Sep 26, 2017):

@escopecz can you do a ->getSQL() too?

@Ocramius commented on GitHub (Sep 26, 2017): @escopecz can you do a `->getSQL()` too?
Author
Owner

@escopecz commented on GitHub (Sep 26, 2017):

@Ocramius doesn't seem I can:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Doctrine\ORM\QueryBuilder::getSql()

We're on "doctrine/orm": "~2.5.4"

@escopecz commented on GitHub (Sep 26, 2017): @Ocramius doesn't seem I can: ``` Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Doctrine\ORM\QueryBuilder::getSql() ``` We're on `"doctrine/orm": "~2.5.4"`
Author
Owner

@Ocramius commented on GitHub (Sep 26, 2017):

@ebernhardson you should do $queryBuilder->getQuery()->getSql() annd dump that to check if the condition is applied.

@Ocramius commented on GitHub (Sep 26, 2017): @ebernhardson you should do `$queryBuilder->getQuery()->getSql()` annd dump that to check if the condition is applied.
Author
Owner

@escopecz commented on GitHub (Sep 26, 2017):

Thanks for the guidance. Here is the SQL if I specify the id and object in the select clause which is returning the correct results:

SELECT l0_.id AS id_0, l0_.object AS object_1 FROM lead_fields l0_ WHERE l0_.object = ? LIMIT 30 OFFSET 0

This is without the object column specified which is returning all fields of all objects:

SELECT l0_.id AS id_0 FROM lead_fields l0_ WHERE l0_.object = ? LIMIT 30 OFFSET 0
@escopecz commented on GitHub (Sep 26, 2017): Thanks for the guidance. Here is the SQL if I specify the id and object in the select clause which is returning the correct results: ``` SELECT l0_.id AS id_0, l0_.object AS object_1 FROM lead_fields l0_ WHERE l0_.object = ? LIMIT 30 OFFSET 0 ``` This is without the object column specified which is returning all fields of all objects: ``` SELECT l0_.id AS id_0 FROM lead_fields l0_ WHERE l0_.object = ? LIMIT 30 OFFSET 0 ```
Author
Owner

@Ocramius commented on GitHub (Sep 26, 2017):

@escopecz what is the resultset like, and what did you expect?

Query looks fine, by the way.

@Ocramius commented on GitHub (Sep 26, 2017): @escopecz what is the resultset like, and what did you expect? Query looks fine, by the way.
Author
Owner

@escopecz commented on GitHub (Sep 26, 2017):

Ok, I think it's not a doctrine bug. The entities will return the default value if the column isn't fetched from the database. So it is returning company fields, but object is lead because that's the default value.

@Ocramius Thanks for helping me realize that!

@escopecz commented on GitHub (Sep 26, 2017): Ok, I think it's not a doctrine bug. The entities will return the default value if the column isn't fetched from the database. So it is returning `company` fields, but object is `lead` because that's the default value. @Ocramius Thanks for helping me realize that!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5717