I have a Doctrine2 project with three tables: Product, Category and Rating. Each Product has a one and only one Category. A Category may have a parent and many children.
Each Product has many Rating.
I run the following query :
SELECT DISTINCT p, AVG(r.value)*COUNT(r) as globalrating
FROM AcmeProductBundle:Product p
JOIN p.ratings r JOIN p.category cat JOIN cat.parent par
WHERE par = '.$categoryID.' OR cat = '.$categoryID.'
ORDER BY globalrating DESC, p.name ASC
where $categoryID is the id of the category where I’m searching for the products.
The point is that, notwithstanding there are no Products in the category, one result is reported. In particular, the object is of type Acme\ProductBundle\Product and has no values into the fields and the id is 0! Also the “globalrating” value is empty! I don’t understand why?
Any idea?
Thanks in advance.
I come back on the problem today and I found the problem, finally!
Reading the related Doctrine docs, I discovered that a
GROUP BYclause where missing. So, the right DQL query is: