group不在CakePHP中工作

时间:2014-03-30 09:21:11

标签: cakephp grouping

我在CakePHP中运行以下查询:

$options = array('conditions' => array('Patient.' . $this->Patient->primaryKey => $id),array('recursive'=>2,'group'=>array('group_id')));
$this->set('patient', $this->Patient->PatientTest->find('all', $options));

但我的group根据需要无效。它给出了如下输出:

Array
(
    [0] => Array
        (
            [PatientTest] => Array
                (
                    [patient_id] => 2
                    [test_id] => 1
                    [date] => 2014-03-27 17:44:17
                    [result] => 55
                    [group_id] => 1
                )

            [Patient] => Array
                (
                    [id] => 2
                    [name] => Test Patient
                    [age] => 44
                    [gender] => 0
                    [email] => emailid@gmail.com
                    [contact] => 789654123
                    [date] => 0000-00-00 00:00:00
                )

            [Test] => Array
                (
                    [id] => 1
                    [name] => Hb
                    [group_id] => 1
                    [normal] => 12 - 16 gma%
                )

            [Group] => Array
                (
                    [id] => 1
                    [name] => Haematology
                )

        )

    [1] => Array
        (
            [PatientTest] => Array
                (
                    [patient_id] => 2
                    [test_id] => 2
                    [date] => 2014-03-27 17:44:17
                    [result] => 55
                    [group_id] => 1
                )

            [Patient] => Array
                (
                    [id] => 2
                    [name] => Test Patient
                    [age] => 44
                    [gender] => 0
                    [email] => emailid@gmail.com
                    [contact] => 789654123
                    [date] => 0000-00-00 00:00:00
                )

            [Test] => Array
                (
                    [id] => 2
                    [name] => PCV
                    [group_id] => 1
                    [normal] => 35-50%
                )

            [Group] => Array
                (
                    [id] => 1
                    [name] => Haematology
                )

        )
)

我需要group_idGroup.id分组。

我无法找到为什么不进行分组。

2 个答案:

答案 0 :(得分:3)

$options = array
(
    'conditions' => array
    (
        'Patient.' . $this->Patient->primaryKey => $id
    ),         
    array(
        'recursive'=>2,
        'group'=>array('group_id')
    )
);

$options = array
(
    'conditions' => array
    (
        'Patient.' . $this->Patient->primaryKey => $id
    ),         
    'recursive'=>2,
    'group'=>array('group_id')
);

递归,条件和群体处于同一水平

答案 1 :(得分:0)

简单而甜蜜的代码:

$groupBy = 'Patient.group_id'; 
$options = array (
        'conditions' => array
            (
            'Patient.' . $this->Patient->primaryKey => $id
            ), 
        'group'=>array($groupBy)
    );