Active Record mix with sql query

Hi guy,

i will show the location list without duplication info inside. How to do it?? How to distinct the location list with findAll()?


    public function getLocationOptions()

    {

        return

        CHtml::listData(company::model()->findAll(), 'id', 'location');

    }

Pls help. Thx

Maybe there is a way you can specify it in findAll. But you can surely use CDbCriteria


$criteria = new CDbCriteria();

$criteria->distinct = true;

CHtml::listData(company::model()->findAll($criteria), 'id', 'location');

You were almost there:


CHtml::listData(company::model()->findAll(array('select'=>'id, location', 'group'=>'location'))), 'id', 'location');

for more info look at:

Working withg AR

Yii find*()

Yii CDbCriteria

Mukesh, SELECT DISTINCT only filters exactly identical datasets. If you want to filter a dataset by a single field You need to group the dataset by that field.

:wink:

Thank Kiriakos Kappa Krastillis and Mukesh for suggestion. Kiriakos’s solution work every well. Thank man. I learn new thing today. XD

Thanks for the correction. don’t know what was i thinking <feeling stupid> :)