saving a has_one relationship

Hi…

My User class has the following relationship.




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'userFriends' => array(self::HAS_ONE, 'UserFriends', 'uid'),

		);

	}



and the UserFriends class has




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'User' => array(self::BELONGS_TO, 'User', 'uid'),

		);

	}



The User is being saved to database but not the UserFriends.




$userFriends = new UserFriends();

$userFriends->uid = $id;

$userFriends->friends = $results;

$model->userFriends=$userFriends;

$model->save();

$model->userFriends->save();



Did I do it correctly??

i dont think you did

something like




$userFriends = new UserFriends();

$userFriends->uid = $id;

$userFriends->friends = $results;

$userFriends->save();//save the friends


//$model->userFriends=$userFriends; --- no need for this line


$model->save();//save the user mode


//$model->userFriends->save(); -- no need for this line either, you saved the friends above



hmmm…still no luck… no error message also :(

by the way, why there is no need for this?




//$model->userFriends=$userFriends; --- no need for this line



because , if you didnt use it before, it will load on the fly, at the time you first use the relation variable

So if you didnt use it before there’s no need to update it, when you use the new content is saved already and updated

about the problem is stil ocurring, I’ve no idea why and can only help you more if you add content, because its seens right or you will have to wait for other ppl give it a try

What other content will be useful?

I populate the User model first and then populate the UserFriends.




$model = new User();

...

$userFriends = new UserFriends();

$userFriends->uid = $id;

$userFriends->friends = $results;


$userFriends->save();

$model->save();



I did both save() but only the User gets saved…

Is there any error debugging I can do?

its probably the data that is not beeing validated

the method save tries to validate before save,

Try


$userFriends->save(false)  //(dont validate)

and if it saves its a validation problem

That saved the day!

There is a foreign key constraint failed lol…

But the error only showed up when I did as you mentioned.




$userFriends->save(false)



Thanks!