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'),)
?