Confused about saving

hi there

i’m really confused and frustrated saving a relational record. been bashing my head against this for days.

i have a model $artist and a model $aliases.

so in artists:

public function relations()


{


	return array(

‘aliases’ => array(self::HAS_MANY, ‘Aliases’, ‘artist_id’)

}

so the relations are working fine.

i then load the $artists model and go and fetch data to update it. all elements of aliases are "safe":

public function rules()


{


array('id, artist_id, name, rank', 'safe', 'on'=>'search')


    }

the fetch gets the data from an api and not from a form - most of what’s in the forum relates to forms.

if i do a print_r of the $artist->aliases after i load the model, i get a huge amount of other information, which i assume by looking at it, it’s the AR data. (i assume this is normal in Yii???)

so far so good.

once i fetch my new data, i want to update the $aliases table.

here i have tried two methods:

  1. just returning the new values for aliases. which is $aliases[‘name’] and $aliases[‘rank’]. i have also manually inserted $aliases[‘artist_id’], because not sure Yii does this automatically.

  2. passing the whole $artist model into my alises class and then returning the whole model (with all those extra arrays) with the new values set and then tried to save it.

i then try to save the model.

tried various things, nothing works.

the curious thing is, it still fires the view:

with if($model->save()) or if($model->save(false)) // to see if it’s a validation error

i’ve also tested to see if it’s a validation error but get no error.

if i do a trace in the view. the new values have been passed to the model. they are just not being saved??!!!

i’ve tried:

$model->save();

$model->save(false);

$model->save('aliases);

$model->update();

$model->update(false);

$model->update('aliases);

$model->setAttributes(‘aliases’, $aliases);

$model->attributes = $aliases;

any help would be appreciated.

thanks

UPDATE::

i did manage to get the saving working with the yii-save-relations-ar-behavior extension:

http://code.google.com/p/yii-save-relations-ar-behavior/

using:

$model->setRelationRecords(‘aliases’,$aliases);

it saves fine. but there’s one issue:

when i do a print_r($model->errors);

i do get an error if the record already exists:

Array ( [aliases] => Array ( [0] => An error occured during the save of aliases ) )

would this be normal?

am i on the right track? i would’ve expected active record to allow me to do this without the extension?

You should be able to save using a master-detail relationship without using an extension (I’m using Yii 1.1.8 and it works). To test, try adding a dummy action to your controller:




public function actionTest()

{

  $artistModel = Artist::model()->with('aliases')->findAll();

  $alias = $artistModel[0]->aliases[0];

  $alias->name = 'Testing Testing Testing';

  $alias->save();

}



Execute your test action using


http://yoursite/index.php?r=artist/test

and then take a look at your database to see if changes are saved.