Update and Insert with Relational Active Record

Hi.

I have noticed that there is a horde of documentation about the different query options when using relational active record, but there is no mention whatsoever about creating and updating records using this method. From the tests that I have done, it would appear that `new’ models and ->save() is unable to update the related tables.

Is this correct, or have I incorrectly defined my relationships?

Example tables:

[sql]

– address table

CREATE TABLE address (

address_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,


address_line_1 VARCHAR(20) NOT NULL DEFAULT '',


address_line_2 VARCHAR(20) NOT NULL DEFAULT '',


city VARCHAR(20) NOT NULL DEFAULT '',


postcode VARCHAR(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> NOT NULL DEFAULT '',


PRIMARY KEY (address_id)

);

– user table

CREATE TABLE user (

user_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,


address_id INT(10) UNSIGNED,


user_name VARCHAR(20) NOT NULL,


password CHAR(32) NOT NULL DEFAULT '',


first_name VARCHAR(20) NOT NULL DEFAULT '',


last_name VARCHAR(20) NOT NULL DEFAULT '',


telephone_number VARCHAR(16) NOT NULL DEFAULT '',


alt_telephone_number VARCHAR(16) NOT NULL DEFAULT '',


admin_user ENUM('Y','N') NOT NULL DEFAULT 'N',


PRIMARY KEY (user_id),


CONSTRAINT user_in_address_mva


  FOREIGN KEY (address_id) REFERENCES address(address_id)

);

[/sql]

User Model:




class User extends CActiveRecord

{

    ...

    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(

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

        );

    }

    ...



I have tested in the following way using the yiic shell:




yiic> $user = new User

yiic> $user->user_name = 'user1'

yiic> $user->first_name = 'FirstName'

yiic> $user->address->address_line_1 = '10 Downing Street'

yiic> $user->address->postcode = 'SW1A 2AA'

yiic> $user->save()



Check:




yiic> $user = User::model()->findByPk(1) // assuming the above inserted the row with user_id = 1

yiic> echo $user->address->address_line_1



Essentially, creating a new record results in only a row being added to the user table, but the address table remains untouched. I have tried adding a BELONGS_TO relationship to the Address model too, but this does not make any difference.

Assuming that insert and update are not possible, can someone recommend the best way of populating a user and the related address in a single form. I can’t seem to find any documentation that covers this.

TIA,

Paul.

Try doing





$user = new User

$user->user_name = 'user1'

$user->first_name = 'FirstName'

$user->address->address_line_1 = '10 Downing Street'

$user->address->postcode = 'SW1A 2AA'

$user->address->save();

$user->address_is=$user->address->primaryKey;

$user->save()



Thanks zaccaria.

Your advice prompted some further testing, and I’ve now found this approach which seems to work:




yiic> $user = new User

yiic> $user->user_name = 'user1'

yiic> $user->first_name = 'FirstName'


yiic> $user->address = new Address

yiic> $user->address->address_line_1 = '10 Downing Street'

yiic> $user->address->postcode = 'SW1A 2AA'

yiic> $user->address->save()


yiic> $user->address_id = $user->address->address_id

yiic> $user->save()



Creating a new User object does not appear to cause the related Address object to be created.

It also appears that when you perform and update, you need to call save() on all the related objects…

Is anyone able to confirm whether or not this is the correct way to make use of relational active record?

Many thanks.

Paul.

I’m desperate for more usable advice like this on all aspects of working with one-many and many-many relationships with the wonderful Yii framework. Some examples showing of the basic capabilities by some of the gurus in the forum would be really, really appreciated.

Thanks to all who have contributed in this space to date.

there is a cookbook recipe on how to create a form and save 2 models

http://www.yiiframework.com/doc/cookbook/19/

maybe this helps

I don’t had saves record, becouse I don’t have parameter (false) on metod $user->save(). After set this parameter and call metod beforeSave() - all well. :)