Yii活动记录如何在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]
])
;
}
你总是可以循环CActiveDataProvider的data属性。Data是一个简单的模型数组
首先你需要把它存储在变量:
$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;
我希望这是你需要的