Scenario


(Aravind Raj) #1

I created a table ‘userfacility’ which comprise fields status, last_name,userid, facilityid, created. I made facilityid and last_name as mandatory in the model (array(‘facilityid,last_name’, ‘required’)).

Then I tried to save a record for ‘userfacility’ through other controller by.

                        After $model->save();





                        if ($_POST['User']['facility_id'] >0)


                        {


                        $update_facility = new UserFacility('udf');


                        $update_facility->userid = $model->id;


                        $update_facility->facilityid = $model->facility_id;


                        $update_facility->save();


                        }

For this I created a rule with scenario in the ‘userfacility’ model. Like this

array(‘userid, facilityid’, ‘required’,‘on’=>‘udf’),

But it doesn’t work it still looks for the old rule with last_name and facility_id. I don’t understand why?


(Kburton) #2

The scenarios only add rules to the existing rules, so all of your existing rules still apply.


(Aravind Raj) #3

Thanks Keith.

But I added a new scenario

array(‘userid, facilityid’, ‘required’,‘on’=>‘udf’),

in the ‘userfacility’ model.

and used it in the controller,

$update_facility = new UserFacility(‘udf’);

Why it didn’t work?


(Aravind Raj) #4

One more thing, If I change the default scenario with some name like

(array(‘facilityid,last_name’, ‘required’ , ‘user’)).

The second scenario which I created also works()

array(‘userid, facilityid’, ‘required’,‘on’=>‘udf’),

Does it means we can’t use scenario’s to save records, unless we gave a name to the default scenario?


(Kburton) #5

I’m not sure what’s not working for you, but if you have rules that aren’t specific to a scenario, then they will apply to all scenarios. In newer versions of Yii, you can use the except clause of the rule to prevent the rule being applied on specific scenarios.