Yii Active Record如何为DataProvider中的每个模型安装场景

时间:2014-08-20 18:42:03

标签: php database activerecord yii

Yii Active Record如何为DataProvider中的每个模型安装方案?

public function searchView()
            {
                $criteria = new CDbCriteria([
                    'select' => 'last_comment,name,data,cdate,mdate,edate,coperator,eoperator',
                    'with' => ['houses', 'houses.city', 'houses.district', 'houses.street','stages']
                ]);
               $model = $this;
               //$model->scenario = 'searchView'; this is for dataprovider
                return new CActiveDataProvider($model , [
                    'criteria' => $criteria,
                    'sort' => [
                        'defaultOrder' => 'cdate DESC'
                    ],
                    'pagination' => $this->searchFrom || $this->searchTo || $this->district_key || $this->city_key ? false : ['pageSize' => 100]
                ])

;
                }

1 个答案:

答案 0 :(得分:1)

您始终可以遍历CActiveDataProvider的数据属性。数据是一个简单的模型数组

首先你需要将它存储在变量中:

$dataProvider = new CActiveDataProvider($model , [
                    'criteria' => $criteria,
                    'sort' => [
                        'defaultOrder' => 'cdate DESC'
                    ],
                    'pagination' => $this->searchFrom || $this->searchTo || $this->district_key         || $this->city_key ? false : ['pageSize' => 100]
                    ]);

比你可以循环模型:

foreach($dataProvider->data as $model){
    $model->scenario = 'searchView';
}

比您可以在每个模型上使用方案返回数据提供者

return $dataProvider;

我希望那就是你需要的东西