MySQL Foreign key concern in gii generated model

I’ve noticed something in the foreign key representations in one of my gii generated models, namely with a table that is referred to more than once from another table.

First, let me give two table declarations.




create table tbl_user

(

    id int unsigned not null auto_increment,

    

    email varchar(255) unique not null,

    username varchar(100) unique not null,

    password varchar(50) not null,

    last_logged_in datetime,

    created_datetime datetime,

    updated_datetime datetime,

    

    primary key(id)


)engine=innodb;


create table tbl_source

(

    id int unsigned not null auto_increment,

    name varchar(100) not null,

    description varchar(255),

    created_datetime datetime,

    updated_datetime datetime,

    created_user_id int unsigned,

    updated_user_id int unsigned,

    

    foreign key(created_user_id) references tbl_user(id),

    foreign key(updated_user_id) references tbl_user(id),


    primary key(id)


)engine=innodb;



Notice that tbl_source has two foreign keys referring to the tbl_user table.

Now let’s look at their representations in their respective models.

Source




public function relations()

{

    return array('createdUser' => array(self::BELONGS_TO, 'User', 'created_user_id'),

		 'updatedUser' => array(self::BELONGS_TO, 'User', 'updated_user_id'),

                );

}



Great, so far so good. The two user foreign keys are represented. But when I look at the relations in the User model,




public function relations()

{

    return array('sources' => array(self::HAS_MANY, 'Source', 'updated_user_id'),);

}



only the updated_user_id reference is listed and not the created_user_id. Why is this?

Can I simply add the element


 'sources' => array(self::HAS_MANY, 'Source', 'created_user_id'),)

?

In my opinion, I think yes, you can just put it down if you really need it though.

Thanks for the opinion.

I think I should be a little clearer.

This isn´t going to work.




public function relations()

{

    return array('sources' => array(self::HAS_MANY, 'Source', 'updated_user_id'),

                 'sources' => array(self::HAS_Many, 'Source', 'created_user_id'),);

}



But what about the following?




public function relations()

{

    return array('sources' => array(array(self::HAS_MANY, 'Source', 'updated_user_id'),

                                    array(self::HAS_Many, 'Source', 'created_user_id'),),);

}



Could I get away with that in the generated model? I ask because the created_user_id reference wasn’t generated

originally for the User model. This arises when a table(in my case tbl_user) has two foreign keys that refer to the same table. I just need to know how Yii would represent it, but since it didn’t generate it, I’m left confused. The array above is syntactically correct, but I don’t know if Yii will interpret it as I intended.

¿Me dejo entender? jeje

First of all, you have to decide if you will need that relation… ie. will you display all sources that a user created or updated…

if you need them, then you have to give different names to the relations… if you use one name…

how will the framework know what you want to display when you give… $model->sources->name is it for created or for updated record

So you create to relations with different names




return array(

   'sourcesUpdated' => array(self::HAS_MANY, 'Source', 'updated_user_id'),

   'sourcesCreated' => array(self::HAS_MANY, 'Source', 'created_user_id'),

);



That way you can use $model->sourcesUpdated->name and $model->sourcesCreated->name

Yeah, that makes sense. I was just concerned how to represent it in Yii. I knew I was going to have to configure something after the gii generation. Thanks a lot. Solved.