Skip to content
Advertisement

How to select distinct query using symfony2 doctrine query builder?

I have this symfony code where it retrieves all the categories related to a blog section on my project:

JavaScript

This works, but the query includes duplicates:

JavaScript

I want to use the DISTINCT command in my query. The only examples I have seen require me to write raw SQL. I want to avoid this as much as possible as I am trying to keep all of my code the same so they all use the QueryBuilder feature supplied by Symfony2/Doctrine.

I tried adding distinct() to my query like this:

JavaScript

But it results in the following error:

Fatal error: Call to undefined method DoctrineORMQueryBuilder::distinct()

How do I tell symfony to select distinct?

Advertisement

Answer

you could write

JavaScript

as

JavaScript

thing is, I am just currently myself getting into Doctrine, so I cannot give you a real answer. but you could as shown above, simulate a distinct with group by and transform that into Doctrine. if you want add further filtering then use HAVING after group by.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement