How to save related objects?

Hi,

I am fighting with Yii to understand the way it works. In the book I can’t find an example about how to save, delete and create related objects of a database object.

Example: I have a User and an Address. Assuming relations are set up, I want to create a user, create an address and save the relation at the same time.

How can I do this?

I thought there would be something like




$user = new User;

$user->name = 'Mike';

$address = new Address;

$address->street = 'Main road 27';

$user->save( $address );



Sure the coding does not match Yiis way of handling, but I wonder how I can do this? Same for Deleting, e.g. I want to delete a user and delete his address at the same time. Ok, this I guess I would do by ForeignKey definition.

Anyway, is there a more detailed how to somewhere about basic AR operations in combination with relationships and such?

Thanks for your help!

I agree with you. I miss a how-to about relations and how to save data when it comes from a form.

Lets assume the Address table has a foreign key pointing to the User table.

More on relations: http://www.yiiframework.com/wiki/181/relations-belongs_to-versus-has_one/

Database:


tbl_user

id primary

first_name varchar

.

.




tbl_address

id primary

user_id foreign key

street varchar

.

.



User Model




public function relations()

{

        return array(

                'address'=>array(self::HAS_ONE, 'Address', 'user_id '),

        );

}



Address Model




public function relations()

{

        return array(

                'user'=>array(self::BELONGS_TO, 'User', 'user_id '),

        );

}



In most cases, each model will have it’s own html form. But in your case, you’ll probably want a form to manage the user and his/her address. In your controller you’ll have to perform 2 saves - 1 for the User and 1 for the Address.




$user = new User();

$user->first_name = 'Mike'; // Or get the data from the submitted form.


if ($user->save())

{

    $address = new Address();

    $address->street = '125 King St.'; // Or get the data from the submitted form.

    $address->user_id = $user->id; // Link by ids.

    $address->save();

}



If you really can’t stand performing 2 inserts manually, you can look at http://www.yiiframework.com/extension/eadvancedarbehavior/. It manages the relations between models.

Cheers,

Matt

There exists a lot of extensions:

  • esaverelatedbehavior

  • xrelationbehavior

  • eadvancedarbehavior

  • esaverelatedbehavior

  • save-relations-ar-behavior

You have to find out which is the best for your needs.

Hello, just few days working with yii. Have a simple problem: can’t save the object using the way above. I have a Gallery, and during its creation I want to attach a cover which is new Picture. actionCreate():


if(isset($_POST['Gallery']))

{

	$model->attributes=$_POST['Gallery'];

	if($model->save()){

		$cover = new Picture();

		//$cover->image = CUploadedFile::getInstance($model, 'cover');

		$cover->title = 'pic12';

		$cover->filename = 'gaga.jpg';

		//$cover->gallery_id = $model->id;

		$cover->save();

		$this->redirect(array('view','id'=>$model->id));

	}

}

Have left only NOT NULL fields to fill. No errors getting but when gallery is created nothing is saved in picture table.

Please help a newbie)))

Hi!

Read this wiki, especially the actionCreate(). Also read the comments about the FK’s.

Thanks! Already have read that article, but not properly. It’s clear now, trouble was that Picture hadn’t pass the validation.