seniorpreacher
seniorpreacher

Reputation: 666

Yii scenario based safe attributes

I have a Product model with the following rules:

...
array('normal_price, company_id, purchase_price', 'safe', 'on' => 'adminList'),
array('normal_price, company_id', 'safe', 'on' => 'list'),
...

And I call the following function:

$product->scenario = 'list';
$product->safeAttributeNames;

The code above should only return ['normal_price, company_id'], but I get the purchase_price as well, all time. Even if I delete the first rule for the adminList scenario.

I would need the list of attributes, that I specify. What could be the problem with my code?

Upvotes: 1

Views: 1391

Answers (2)

seniorpreacher
seniorpreacher

Reputation: 666

I just found out, that I had a length rule for the purchase rule without scenario. When I commented out every other rules, it worked what I wanted.

Description:

To only get the 'safe' attributes, you should not have any other rule for those attributes in that scenario.

My solution was to set the default scenarios to every rule like:

array('id', 'require', 'on' => 'insert, update')

Upvotes: 1

Pitchinnate
Pitchinnate

Reputation: 7556

Try using this:

$product->setScenario('list');

Upvotes: 0

Related Questions