문제

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,

        ));
도움이 되었습니까?

해결책

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',
    ),

다른 팁

Try this

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

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

$model = SomeModel::model()->findAllByPk($idArray , $criteria);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top