doushan3511 2014-07-19 11:11
浏览 43

CDbException外键的格式必须是“joinTable(fk1,fk2,...)”

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题