Scenario explanation

Hi guys, thank you for reading.

I’m trying to figure out the whole thing of scenario. It is quite obvious when you mess with rules and controller logics, but I’m missing the point when it comes to some automated model generation.

It is the case of using the Model::model()->find* methods. Well, when I’ll insert a record and create a new Model(‘insert’) it is very strait-fw. But when you ask to find an PK and it returns model->scenario = ‘update’ scares me a bit.

Till now I’ve seen two ‘self-implemented’ model scenarios (update and insert). Is there more?

In fact, I’ve found the whole thing quite strange. I love rules, but this sort of ‘don’t know whats happened’ freaks me. This feeling may be the lack of complete documentation on the matter or the complexity of AR.

Someone can pull out some thoughts and try to clear my mind of this darkness? :)

Thank you very much.

Besides insert and update, you get to define your own scenarios and these are mostly used for validation rules.

lets says you have a model X




class modelX extends CActiveRecord {




public function rules(){

		

  return array(

    array('user_email, username', 'required'),

    array('avatar', 'required', 'on'=>'account_settings'),

  );

}




only user_email and username are validated in any scenario when calling validate() or save() the record;

lets say you have

$model->setScenario('account_settings);

and then call

$model->save();

all 3 attributes will be validated.

you could also use the scenario to do other things, for example rendering a field in a form

lets say that when login in you only want to display the username and email field

then do something like




$model->setScenario('login');

// print the username field


// print email field


if($model->getScenario() == 'account_settins')

// print avatar field




Thanks for the explanation mate, I read alot about scenarios and i didnt quite get them until this :) bookmarked, Thanks again cheers!