Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP 2.1 - How to properly use DISTINCT in find()
    primarykey
    data
    text
    <p>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, <a href="https://stackoverflow.com/questions/1718482/using-distinct-in-a-cakephp-find-function">Using DISTINCT in a CakePHP find function</a>, using DISTINCT this way:</p> <pre><code>$this-&gt;Model-&gt;find('all', array('fields'=&gt;'DISTINCT field_name')); </code></pre> <p>does not return DISTINCT values, instead it returns all rows. In fact, the DISTINCT here is completely pointless because, for some reason , CakePHP adds <code>TableName</code>.<code>id</code>in the SQL query (why?? can I remove the id reference??), effectively returning every DISTINCT primary key (=all rows=unhelpful). </p> <p>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()?</p> <p>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:"</p> <pre><code>&lt;?php array( 'fields' =&gt; array('DISTINCT (User.name) AS my_column_name'), 'order' = &gt;array('User.id DESC') ) ?&gt; </code></pre> <p>Source: <a href="http://book.cakephp.org/2.0/en/models/retrieving-your-data.html" rel="nofollow noreferrer">http://book.cakephp.org/2.0/en/models/retrieving-your-data.html</a></p> <p>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? </p> <p>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?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload