I have a question which is driving me crazy and I have to admit I am not that experienced in CakePHP. As mentioned in this question, Using DISTINCT in a CakePHP find function, using DISTINCT this way:
$this->Model->find('all', array('fields'=>'DISTINCT field_name'));
does not return DISTINCT values, instead it returns all rows. In fact, the DISTINCT here is completely pointless because, for some reason , CakePHP adds TableName
.id
in the SQL query (why?? can I remove the id reference??), effectively returning every DISTINCT primary key (=all rows=unhelpful).
So, I still want to return the DISTINCT values of a particular field_name column. Can I not do it using just the find('all') or find('list') function? Is it really that the proper way of doing it using this Set::extract() function described in the link above? That appears to be a overly indirect solution by CakePHP, normally Cake make my life easier. :-) What is the proper way of using find and DISTINCT together? Maybe DISTINCT doesn't work for find()?
Looking at the CookBook, they say: "A quick example of doing a DISTINCT query. You can use other operators, such as MIN(), MAX(), etc., in a similar fashion:"
<?php
array(
'fields' => array('DISTINCT (User.name) AS my_column_name'),
'order' = >array('User.id DESC')
)
?>
Source: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
This indicates that DISTINCT should be possible to use, but what is what here? Does (User.name) correspond to the field_name I want DISTINCT for or is my_column_name my field_name?
Finally, has any of this changed when migrating from CakePHP 1.x to CakePHP 2.x? Ie are the answers for CakePHP 1.x seen on Stackoverflow still relevant?
Thanks in advance!
$regions = $this->Provider->find('all', array ('fields' => array('DISTINCT (provider.region) AS region')));
or$regions = $this->Provider->find('all', array ('fields' => array('DISTINCT (provider.region)')));
it doesn't pick up unique entries. Instead all rows which is not DISTINCT and not what I wanted. The resulting SQL query:SELECT DISTINCT (
provider.
region),
Provider.
id` FROMcarecrowd
.providers
ASProvider
WHERE 1 = 1`. Did I missunderstand you somehow and is my code incorrect? – Javier