Update with or whitout scenario is nearly the same.
The code you should use is the usual CRUD code, the only difference that scenarios does are validators.
Changing the list of validators means change safe attribute, and so you can protect data (some field that in some scenarios you don’t want to expose to modify) by changing the validation rules.
What exactly confuse you? Do you need some examples? About what?
but I couldn’t work out how to tell the update to only process the fields in the scenario. With an update you have existing data to use in the model ie…
I couldn’t work out where the scenario should be set. I tried $model->validate(‘updateprofile’) as well but I was getting totally confused by that point.
I have got another question about the validation rules and setting some outside the scenario but one at a time!
setScenario works! I’ve pasted the full update function below.
This also works when you have other rules set… I’ve had a problem before with other rules preventing the scenario rule from functioning.
public function actionUpdate()
{
$model=User::model()->findbyPk(Yii::app()->user->getId());
$model->setScenario('updateprofile'); // rules set in model for updating profile
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save()){
$this->redirect(array('view','id'=>Yii::app()->user->getId()));
}
}
$this->render('update',array(
'model'=>$model,
));
}