【问题标题】:How to install the scenario in ActiveRecord DadaProvider for each model如何在 ActiveRecord DadaProvider 中为每个模型安装场景
【发布时间】:2014-08-20 09:02:14
【问题描述】:

如何在 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'
        ]);
}

【问题讨论】:

  • 如果我理解得很好您希望 CActiveDataProvider 检索到的所有模型都具有 searchView 场景吗?
  • 是的!这很重要

标签: php activerecord orm yii


【解决方案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'
]);

【讨论】:

  • 天啊!你帮了我很多,我做不到,做同样的事情,但是没有调用方法$model->afterFind()
  • 你不知道为什么GridView返回白屏?
  • 很难知道。在日志中,sql查询是否按原样执行?
  • 完全黑屏,甚至没有工具栏和html干净。控制台错误加载资源失败:net::ERR_CACHE_MISS
  • 如果你再把默认的数据提供者你仍然有同样的错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
相关资源
最近更新 更多