Question

I'm using CAdvancedArBehavior to save MANY_MANY relationships in my Yii application. I'm using a listBox with "multiple"="multiple" enabled to select the related objects, whose IDs are being passed as values. For some reason, when I try to save my model, the related objects aren't being saved, but everything else is.

I've used print_r to print out my POST data to check that everything is passing through, and it seems to be. I can even load my object in the yiic shell and add the same array of MANY_MANY object ids and have it save properly.

Is there something I need to enable to use this extension normally? I'm wondering if maybe the yiic shell automatically loads extension that need some manual action of some sort in the main application?

By the way, my form is created with the CActiveForm widget, so my fields definitely all match up to the object's properties.

Was it helpful?

Solution

Basically, when sending associated models through CActiveForm, even though the form accepts it as a field, when you assign $_POST['Model'] to $model->attributes, it doesn't include the associated models (obvious, when I think about it, I guess).

Basically, this extra line is necessary to save the associated models:

$model->relation_name = $_POST['Model']['relation_name'];

With that in front of the $model->save(), everything works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top