We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Distinct on Phalcon\Mvc\Model\Criteria

Hi guys, I was wondering if is possible to get distinct values with a query built with the model criteria.

Follow an example (PS.: don't bother with the lack of the bind parameter, it was just a test):

https://gist.github.com/williamokano/a17cb07d791da65bc997

In this case, when I have 1 user that already lived on a city A (id=1) and on a city B (id=2) I will get the user twice (or more, that depends on how many cities he lived).

I know that it's caused because of the intermediate table called lived, and I get the same problem creating "raw queries". But in raw queries I can add the DISTINCT clause and I couldn't find how to do that with the Criteria class.

PS.: I know that if I use the query builder I can set distinct(true) and my problem is solved, but my question is exactly on how to use distinct with Criteria and not with the QueryBuilder.

Regards and apologies about the bad english.



34.6k
Accepted
answer

This is implemented in the 2.0.x branch