【问题标题】:Yii relation throughYii 关系通过
【发布时间】:2013-12-06 01:44:59
【问题描述】:

我有 3 张桌子:

House:
id
name
...

Activity:
id
Title
...

Category:
id
title
...

我想建立一个类似$category->houses 的关系来返回所有在特定类别中有活动的房子。

我尝试了类似的方法:

public function relations() {
    return array_merge(array(
       'houses'=>array(self::HAS_MANY,'House',array('house_id'=>'id'),'through'=>'activite'),
            ), parent::relations());
}

由于我的范围,这给出了一个错误:

public function defaultScope() {
        return array(
            'condition' => "deleted='0'",
       );
}

如果我评论我的范围,它会起作用,但我希望让它们一起工作!

这是错误信息:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'deleted' in where clause is ambiguous. The SQL statement executed was: SELECT `houses`.`id` AS `t1_c0`, `houses`.`titre` AS `t1_c1`, `houses`.`rabais` AS `t1_c2`, `houses`.`slug` AS `t1_c3`, `houses`.`situation` AS `t1_c4`, `houses`.`conseil_utile` AS `t1_c5`, `houses`.`telephone1` AS `t1_c6`, `houses`.`telephone2` AS `t1_c7`, `houses`.`address1` AS `t1_c8`, `houses`.`address2` AS `t1_c9`, `houses`.`address3` AS `t1_c10`, `houses`.`gmap` AS `t1_c11`, `houses`.`link_site_web` AS `t1_c12`, `houses`.`link_facebook` AS `t1_c13`, `houses`.`link_autre` AS `t1_c14`, `houses`.`region_id` AS `t1_c15`, `houses`.`create_id` AS `t1_c16`, `houses`.`create_time` AS `t1_c17`, `houses`.`update_id` AS `t1_c18`, `houses`.`update_time` AS `t1_c19`, `houses`.`deleted` AS `t1_c20` FROM `house` `houses` LEFT OUTER JOIN `activite` `activites` ON (`activites`.`house_id`=`houses`.`id`) WHERE (deleted='0') AND (`activites`.`categorie_id`=:ypl0)

【问题讨论】:

    标签: activerecord yii scope yii-relations


    【解决方案1】:

    错误消息告诉您,您的查询中有多个名为“已删除”的列。您需要指定要与哪个表进行比较删除的列。您可以通过在您的范围内指定这样的方式来做到这一点;

    public function defaultScope() {
            return array(
                'condition' => $this->getTableAlias(FALSE, FALSE).".deleted='0'",
           );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多