Failed to set unsafe attribute

I keep getting a repeated error in my log files when trying to search on a custom field.

Failed to set unsafe attribute "classification_search" of "Questions".

I added it to the safe list so not sure why it’s not working.

[close]

array(‘question,subject_search,classification_search’, ‘safe’, ‘on’=>‘search’),

[/close]

Any ideas?

Thanks

Lux

Did you tried to add it to an ‘unsafe’ list ?

I am pretty sure the problem is that you didn’t instantiate the model with the proper scenario


$model= new Questions();

$model->setAttributes(array('classification_search'=>'foobar')); // won't work


$model= new Questions("search");

$model->setAttributes(array('classification_search'=>'foobar')); // should work

greetings,

Hannes

Thanks Haensel that seems to have worked. Although I’m not sure how or why.

I changed the admin action to instantiate a new Question model just as you advised and it works fine but what’s odd is that the only two fields that were causing the problems where ones that I had too define myself. The one that was straight from the model ‘question’ was working fine.

But when I instantiated like…




$model=new Questions("search");



… the two that weren’t working are now working. Can you explain the difference please so I might get a better understanding of how yii operates.

Thanks again

Lux

PS: I found out how to implement the search from this wiki article http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview - it might be an idea to add this requirement to the article. Just a thought.

Well, by appending “on”=>“search” you specified that the attributes can only be set when the current scenario is “search” which wasn’t the case. If you look at the search() method in your modelclass (if you used gii to create the model) you would see that a new instance is created by passing “search” to the constructor telling the model what the current scenario is.

Take a look at this wiki article to get an idea how scenarios work: http://www.yiiframework.com/wiki/266/understanding-scenarios/

Thanks for the info Haensel. I’ll definitely read up on scenarios.

Lux