I am developing an online store. There are of course a lot of one to many associations.

Let's say I use this DQL: SELECT p, c FROM Entity\Product p LEFT JOIN p.combinations c WHERE p.id = :id The product may contains some combinations but also may not, quite simple.

When I use “in-line” query and getSingleResult() / getOneOrNullResult() everything is fine. It returns the product and for example 5 combinations in $product->combinations.

But when I switch to QueryObject and use fetchOne(), it returns only one combination. It took me a while to find the reason. It is because of setMaxResults(1) settings (it is quite stupid) – it affects associated entities in the result too.

So my question – is it necessary to set this max results limit in fetchOne() method? When I use for example common Doctrine method getSingleResult() I have to take care of number of rows in result of my query too.. (I get the product by its id = primary key so I know it is unique.)

Otherwise I see 2 solutions, both not so nice and clean.

  • fetch() and use the first item of ResultSet
  • do not use QueryObject for these type of queries

I would be grateful for any idea / feedback. Maybe I am doing it all wrong :-) Thanks a lot.

When I use “in-line” query and getSingleResult() / getOneOrNullResult() everything is fine. It returns the product and for example 5 combinations in $product->combinations.

But you have to add ->andWhere('p.id = :id', $productId), right? Because if there is more than one result, it throws, that is the reason for ->setMaxResults(1)

But when I switch to QueryObject and use fetchOne(), it returns only one combination. It took me a while to find the reason. It is because of setMaxResults(1) settings (it is quite stupid) – it affects associated entities in the result too.

Yeah, that's a bug. Well, not exactly. It's expected behaviour. DQL is supposed to act like SQL, so you can optimize better. And fetchOne should return one result.. so there is that.

The right way to solve this is to use the Paginator and make it fetch one result. I've created an issue Kdyby/Doctrine#118

fetch() and use the first item of ResultSet

This is not a solution I would be happy to use

do not use QueryObject for these type of queries

If you need only one root entity, then yes, I would strongly advise you to not create query objects, but rather add some method to your bussines logic class.


You must first log in to participate in this discussion