Gunnit
Gunnit

Reputation: 1074

Why is select count distinct not working in yii?

can someone tell me why is this not working, what i mean is that the select distinct is not working its not counting properly ;

$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
    ),
        ));

Upvotes: 4

Views: 5388

Answers (3)

denisarius
denisarius

Reputation: 21

Digging into Yii's code I figured out that the only way one can specify arbitrary column list for count(distinct <columns>) is to provide 'select' for CDbCriteria as follows:

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

Upvotes: 2

thevikas
thevikas

Reputation: 1639

$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
    ),
        ));

The param name should also be :id_meeting

Upvotes: 3

devBinnooh
devBinnooh

Reputation: 611

count api

public string count(mixed $condition='', array $params=array ( ))

So try to pass the params as an array and to the second count method's parameter.

ex.

$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
    )
 );

Update: count that are combined with ->with do not work properly. A bug has to be reported.

Work Around would be:

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

                        )
                     );

And to debug this try to give constant instead of $data->id_meeting

Upvotes: 1

Related Questions