【发布时间】:2014-09-24 15:17:17
【问题描述】:
我做了很多 HAS_MANY 关系但是..这一次,它不想工作,我不知道为什么。
我有两个模型:
Contest (id, name) 和MpContest (id, contest_id).
在我的数据库中,我创建了外键和主键。
在比赛课上:
public function relations() {
return array(
'mpContests' => array(self::HAS_MANY, 'MpContest', 'contest_id'),
);
}
在 MpContest 类中:
public function relations() {
return array(
'contest' => array(self::BELONGS_TO, 'Contest', 'contest_id'),
'contestType' => array(self::BELONGS_TO, 'ContestType', 'contest_type'),
);
}
当我尝试从竞赛中获取相对 MpContest 时,我得到一个错误。
我尝试这样做:
$mpContests = $this->mpContests;
我明白了:
CDbCommand n'a pas pu exécuter la commande SQL : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mpContests.contest_id' in 'where clause'. The SQL statement executed was: SELECT `mpContests`.`id` AS `t1_c0`, `mpContests`.`name` AS `t1_c1`, `mpContests`.`date_start` AS `t1_c2`, `mpContests`.`date_end` AS `t1_c3`, `mpContests`.`date_start_post` AS `t1_c4`, `mpContests`.`date_end_post` AS `t1_c5` FROM `contest` `mpContests` WHERE (`mpContests`.`contest_id`=:ypl0). Bound with :ypl0='1'
我也尝试添加 'index' => 'id' 但是.. 没有任何改变。
怎么了?
【问题讨论】:
-
你试图在哪个文件中调用 "$mpContests = $this->mpContests;"
-
我尝试在竞赛模型中调用它..
标签: php yii relationship