【问题标题】:Yii has_many relation failed - Column not found: 1054 Unknown columnYii has_many 关系失败 - 未找到列:1054 未知列
【发布时间】: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


【解决方案1】:

天哪!我发现它为什么不想工作了..

我在 MpContest 类中拼错了表名..

这是我的错。对不起,我的粗心大意,也许其他人也会有同样的问题。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2015-02-22
    • 2015-10-26
    • 2016-04-09
    • 1970-01-01
    • 2017-11-07
    • 2020-05-13
    • 2019-01-13
    相关资源
    最近更新 更多