Select distinct with CakePHP

Published on and tagged with cakephp  model  sql  tip

Sometimes you have to use SELECT DISTINCT to get the desired records from the database. How can you use SELECT DISTINCT with CakePHP?

The solution is rather simple:

$this->User->findAll(null, 'DISTINCT User.city');

Or with the array syntax:

$this->User->findAll(null, array('DISTINCT User.city'));

Please notice that the keyword “DISTINCT” has to be uppercase, else you will get the following error:

Unknown column 'distinct User.city'

7 comments baked

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License