我想根据场景返回不同的字段。如何在dataProvider中设置它?
public function fields()
{
if ($this->scenario == 'statistics') {
return [
'id',
'email',
'count'
];
}
return [
'id',
'name'
];
}
用户模型中的字段:
current_space
答案 0 :(得分:1)
只需使用getModels()
方法获取所有模型,在循环中为所有模型设置方案,然后返回数据提供程序。您的示例将更改为以下代码:
$query = User::find();
$activeData = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
]);
foreach($activeData->getModels() as $model) {
$model->scenario = 'statistics';
}
return $activeData;