【问题标题】:Yii Active Record how to install scenario for each models in DataProviderYii Active Record 如何在 DataProvider 中为每个模型安装场景
【发布时间】:2014-08-20 18:42:03
【问题描述】:

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]
                ])

;
                }

【问题讨论】:

    标签: php database activerecord yii


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

    我希望这就是你所需要的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多