如何在yii ActiveRecord DadaProvider中为每个模型安装场景;默认情况下应该更新
public function afterFind()
{
if ($this->scenario === 'searchView') {
//myCode
}
parent::afterFind();
}
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']
]);
return new CActiveDataProvider($this, [
'criteria' => $criteria,
'sort' => [
'defaultOrder' => 'cdate DESC'
]);
}
答案 0 :(得分:1)
我认为你不能在获取对象时指定你想要的场景,我看到的最接近的是扩展CActiveDataProvider
:
class CustomActiveDataProvider extends CActiveDataProvider {
protected function fetchData()
{
$data = parent::fetchData();
foreach($data as $model) {
// do what you wanted to do in the afterFind
// $model->afterFindSearchView()
}
return $data;
}
}
然后在您的模型中创建方法afterFindSearchView
,并在调用数据提供者时执行
return new CustomActiveDataProvider($this, [
'criteria' => $criteria,
'sort' => [
'defaultOrder' => 'cdate DESC'
]);