aopon
(Andrzej)
October 31, 2014, 9:25am
1
Hi,
I’d like to change models scenario while loading data with loadMultiple. Is there an easier way than this?:
$model = MyModel::find()->all();
if($model != null) {
foreach($model as &$m) {
$m->scenario = "my_scenario";
}
}
if(Model::loadMultiple($model, Yii::$app->request->post())) {
...
}
evstevemd
(Stefano Mtangoo)
October 31, 2014, 9:48am
2
Not tested but isn’t scenario attribute of the model? Can’t you do massive assignment just as post?
Model::loadMultiple($model, [‘scenario’=>‘your_scenario’]);
aopon
(Andrzej)
October 31, 2014, 9:53am
3
Not tested but isn’t scenario attribute of the model? Can’t you do massive assignment just as post?
Model::loadMultiple($model, [‘scenario’=>‘your_scenario’]);
Yes it is, only it has to be set ‘safe’ in model class. Thanks
aopon
(Andrzej)
October 31, 2014, 10:18am
4
Now I’m thinkig, passing this value in a form might be dangerous - user can change this attribute. If there is no other way I’ll stay with my first idea.
Thx
evstevemd
(Stefano Mtangoo)
November 3, 2014, 11:05am
5
Just make a function in validation and set fixed list of scenarios as whitelist