Question

i'm using Yii php, and having trouble with finding records with an array of primary keys.

I know that:

$idArray = array(1,2,3);
$model = SomeModel::model()->findAllByPk($idArray);

above code works. But i want to know how to do it in CdbCriteria for I still have other conditions to add, that i cant do in the typical find() methods.

How do i search records with primary keys in CdbCriteria?

--------------Edited---------------------------

I need to create a CActiveDataProvider using the model I retrieved.

return new CActiveDataProvider($model,array(
            'criteria'=>$criteria,

        ));

sadly above doesn't work. Below works.

return new CActiveDataProvider('SomeModel',array(
            'criteria'=>$criteria,

        ));
Was it helpful?

Solution

Given an array
$idArray = array(1,2,3); You can write like this.

$dataProvider=new CActiveDataProvider('Mymodel',array(
             'criteria'=>array(
        'condition'=>'id IN ('$idArray[0].',' .$idArray[1].','.$idArray[2].')',
        'order'=>'id DESC',
    ),

OTHER TIPS

Try this

$idArray = array(1,2,3);

$criteria = new CDbCriteria;
$criteria->condition = "CONDITION_HERE";

$model = SomeModel::model()->findAllByPk($idArray , $criteria);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top