I am fairly new to yii and I have a problem when i reading the "Web Application Develeopment with Yii and PHP" book.
The problem occurred with the book's project in chapter 5, when i create a method of getUserOptions() method in project model class that return an array of valid users for selected project, indexed by user IDs.
The tables are:
tbl_user
CREATE TABLE `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_bin NOT NULL,
`email` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
`last_login_time` datetime DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
tbl_project
CREATE TABLE `tbl_project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_bin NOT NULL,
`description` text COLLATE utf8_bin NOT NULL,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
tbl_project_user_assignment
CREATE TABLE `tbl_project_user_assignment` (
`project_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`project_id`,`user_id`),
KEY `fk_user_project` (`user_id`),
CONSTRAINT `fk_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_user_project` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
and finally the relations defined inside their respective classes as follows:
// model Project
public function relations()
{
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', '{{project_user_assignment}}(project_id, user_id'),
);
}
// model User
public function relations()
{
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'requester_id'),
'issues1' => array(self::HAS_MANY, 'Issue', 'owner_id'),
'tblProjects' => array(self::MANY_MANY, 'Project', '{{project_user_assignment}}(user_id, project_id)'),
);
}
When I call the getUserOptions() method as the following format:
<?php echo $form->dropDownList($model,'owner_id', $model->project->getUserOptions()); ?>
Yii's' CDbException returns an error that says:
The relation "users" in active record class "User" is specified with an invalid foreign key. The format of the foreign key must be "joinTable(fk1,fk2,...)".
So what's the wrong with my code that i actually write from the book?
I useing Yii v.1.1.15 and PHP v.5.4.19