为什么选择计数明显不能在yii中工作?

时间:2013-03-14 11:19:38

标签: select yii distinct

有人可以告诉我为什么这不起作用,我的意思是选择distinct不能正常工作;

$count_participants = BridgeMeeting::Model()->with('idUserRegistry')->count(array(
    'condition' => 'id_meeting=:id_meeting',
    'select' => 'id_user_registry',
    'distinct' => true,
    'params' => array(
        "id_meeting" => $data->id_meeting
    ),
        ));

3 个答案:

答案 0 :(得分:3)

$count_participants = BridgeMeeting::Model()->with('idUserRegistry')->count(array(
    'condition' => 'id_meeting=:id_meeting',
    'select' => 'id_user_registry',
    'distinct' => true,
    'params' => array(
        ":id_meeting" => $data->id_meeting
    ),
        ));

参数名称也应为:id_meeting

答案 1 :(得分:2)

深入研究Yii的代码我发现,为count(distinct <columns>)指定任意列列表的唯一方法是为CDbCriteria提供'select',如下所示:

$ar->count(
'select' => 'count(distinct <columns>)',
'condition' => ...,
'params' => ...
);

答案 2 :(得分:1)

count api

  

公共字符串计数(混合$ condition =&#39;&#39;,array $ params = array())

因此,尝试将params作为数组传递给第二个计数方法的参数。

离。

$count_participants = BridgeMeeting::Model()->with('idUserRegistry')->count(
    array(
        'condition' => 'id_meeting=:id_meeting',
        'select' => 'id_user_registry',
        'distinct' => true,
     ),
    array(
    "id_meeting" => $data->id_meeting
    )
 );

count合并的

更新 ->with无效。必须报告错误。

解决方法是:

                 BridgeMeeting::Model()->with('idUserRegistry')->count(
                     array(
                        'condition' => 'id_meeting=' . $data->id_meeting,
                        'select' => 'id_user_registry',
                        'distinct' => true,

                        )
                     );

要调试此尝试给出常量而不是$data->id_meeting