I’m having some difficulties understanding AR relations. What I want is simple. When I’m managing (actionAdmin) a user, I want to select the user type from a pulldown. I can’t manage to get the data from the foreign table.
I have the following tables:
CREATE TABLE `User` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`usertypeid` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`entry_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `UserType` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
In yiic shell I created the models and crud files.
In User I defined the folowing relation:
public function relations()
{
return array(
'usertypeid'=>array(self::BELONGS_TO, 'UserType', 'id'),
);
}
In UserType the following:
public function relations()
{
return array(
'user'=>array(self::HAS_MANY, 'User', 'usertypeid'),
);
}
In UserController in the actionAdmin function I have this: