Using DISTINCT in a CakePHP find function

后端 未结 12 1888
情歌与酒
情歌与酒 2020-12-06 05:54

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list.

相关标签:
12条回答
  • In some cases, you wish to group by using some key, but you want unique element within the results. For example, you have a calendar application with two types of events. One event on day 1rst and the other one on the 2nd day of month 1. And you want to show or rather count all the events, grouped by day and by type. If you use only DISTINCT, it is quite difficult. The simplest solution is to group twice:

    $this->Events->virtualFields['count'] = 'COUNT(*)';                   
    $acts  = $this->Activity->find('all',array(                   
                 'group' => array('DAY(Event.start)','EventType.id'),
                   ));
    
    0 讨论(0)
  • 2020-12-06 06:05

    You can try this. Here this takes Person id as key, so there is no chance for duplicate entries.

        $first_names = $this->Person->find('list', array(
        'fields' => array('id','first_name'),
        'conditions' => array('Person.status' => '1'),
       ));
        $this->set('first_names', $first_names);
    
    0 讨论(0)
  • 2020-12-06 06:06

    In the example now on the book for version 2 it states the following:

    public function some_function() {
        // ...
        $justusernames = $this->Article->User->find('list', array(
            'fields' => array('User.username')
        ));
        $usernameMap = $this->Article->User->find('list', array(
            'fields' => array('User.username', 'User.first_name')
        ));
        $usernameGroups = $this->Article->User->find('list', array(
            'fields' => array('User.username', 'User.first_name', 'User.group')
        ));
        // ...
    }
    

    With the above code example, the resultant vars would look something like this:

    $justusernames = Array
    (
        //[id] => 'username',
        [213] => 'AD7six',
        [25] => '_psychic_',
        [1] => 'PHPNut',
        [2] => 'gwoo',
        [400] => 'jperras',
    )
    
    $usernameMap = Array
    (
        //[username] => 'firstname',
        ['AD7six'] => 'Andy',
        ['_psychic_'] => 'John',
        ['PHPNut'] => 'Larry',
        ['gwoo'] => 'Gwoo',
        ['jperras'] => 'Joël',
    )
    
    $usernameGroups = Array
    (
        ['User'] => Array
        (
            ['PHPNut'] => 'Larry',
            ['gwoo'] => 'Gwoo',
        )
    
        ['Admin'] => Array
        (
            ['_psychic_'] => 'John',
            ['AD7six'] => 'Andy',
            ['jperras'] => 'Joël',
        )
    
    )
    

    You have to plan your query in a slightly different way and plan your database to accommodate a list find.

    0 讨论(0)
  • 2020-12-06 06:07

    You're right, it seems that you cannot use DISTINCT with list. Since you don't need id but only the names, you can use find all like above and then $first_names = Set::extract($first_names, '/Person/first_name');. That will give you a array with distinct first names.

    0 讨论(0)
  • 2020-12-06 06:07

    Yes the problem is that you are using a listing which designed for a id / value output. You probably will have to do a find('all') and then build the list yourself.

    0 讨论(0)
  • 2020-12-06 06:10

    In 2.7-RC version, this is working

    $this->Model1->find('list', array(
          'fields' => array('Model1.field1', Model1.field1')
    ));
    
    0 讨论(0)
提交回复
热议问题