Cdbcriteria, Using One Column With Distinct Checking


I’d like to form a MySQL query like this with CDbCriteria:

SELECT DISTINCT post_id from post_report ..etc

I’ve tried setting distinct to true for the CDbCriteria:

$postReportCriteria->distinct = true;

Which results a query where all columns are listed after distinct, before “WHERE”. Here’s an example:

SELECT DISTINCT `t`.`id` AS `t0_c0`, `t`.`post_id` AS `t0_c1`, `t`.`reason_id` AS `t0_c2`, `t`.`handled` AS `t0_c3`, ..etc

Manually setting the selection order will result pretty much the same but the order of the columns is different after DISTINCT. Again, all columns listed before WHERE while I’d like to only use one column for distinct checking.

$postReportCriteria->distinct = true;

$postReportCriteria->select = array('post_id', 'id', 'reason_id', 'transaction_id');

This results a slight change in the query, as explained above. Here’s an example:

SELECT DISTINCT `t`.`post_id` AS `t0_c1`, `t`.`id` AS `t0_c0`, `t`.`reason_id` AS `t0_c2`, ..etc 

I really wouldn’t like to use a manual query for this operation as this CDbcriteria is used in a dataprovider.

Any thoughts?

Yii version: 1.1.14

Solved by adding a group to the CDbCriteria.

$postReportCriteria->group = 'post_id';