【问题标题】:Yii Model->search() criteria compare with MANY_MANY relationYii Model->search() 标准与 MANY_MANY 关系比较
【发布时间】:2015-04-21 13:05:30
【问题描述】:

我有三张桌子:

以及其中两个具有 MANY 到 MANY 关系的模型

tbl_social_messages_list:

return array(
    'service' => array(self::BELONGS_TO, 'SocialServices', 'service_id'),
    'mtypes' => array(self::MANY_MANY, 'SocialMessageTypes', 'tbl_social_messages_mtypes_relation(m_id, t_id)' ),
);

tbl_social_message_types

return array(
    'messages' => array(self::MANY_MANY, 'SocialMessages', 'tbl_social_messages_mtypes_relation(t_id, m_id)' ),
);

我正在尝试使用带有 tbl_social_message_types.id 的下拉菜单进行网格过滤以进行比较。

我的搜索:

$criteria=new CDbCriteria;
$criteria->compare('mtypes.id', $this->mType);
$criteria->compare('t.id',$this->id);
$criteria->compare('t.service_id',$this->service_id);
$criteria->compare('t.title',$this->title,true);
$criteria->compare('t.comment',$this->comment,true);
$criteria->compare('t.category_id',$this->category_id,true);
$criteria->with = array('service', 'mtypes');
$criteria->order = 't.id DESC';

return new CActiveDataProvider($this, array(
    'criteria'=>$criteria,
    'pagination'=>array(
        'pageSize'=>50,
    ),
));

但是当我发出请求(更改过滤器字段)时,Yii 会返回 DB 错误:

CDbCommand 未能执行 SQL 语句:SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列 'mtypes.id'。执行的 SQL 语句为:

SELECT `t`.`id` AS `t0_c0`, `t`.`service_id` AS `t0_c1`, `t`.`category_id` AS `t0_c2`, `t`.`title` AS `t0_c3`, `t`.`comment` AS `t0_c4` FROM `tbl_social_messages_list` `t`  WHERE (mtypes.id=:ycp0) ORDER BY t.id DESC LIMIT 50

我明白,表 mtypes 不存在,但我不明白 - 为什么会这样。

【问题讨论】:

    标签: php activerecord yii criteria foreign-key-relationship


    【解决方案1】:

    尝试先放置with 条件:

    $criteria = new CDbCriteria;
    $criteria->with = array('service', 'mtypes');
    $criteria->compare('mtypes.id', $this->mType);
    $criteria->compare('t.id', $this->id);
    $criteria->compare('t.service_id', $this->service_id);
    $criteria->compare('t.title', $this->title, true);
    $criteria->compare('t.comment', $this->comment, true);
    $criteria->compare('t.category_id', $this->category_id, true);
    $criteria->order = 't.id DESC';
    
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'pagination'=>array(
            'pageSize'=>50,
        ),
    ));
    

    如果不行,有时 Yii 需要 'together' => true 参数。

    $criteria = new CDbCriteria;
    $criteria->with = array('service' => array('together' => true),
                            'mtypes'  => array('together' => true));
    $criteria->compare('mtypes.id', $this->mType);
    $criteria->compare('t.id', $this->id);
    $criteria->compare('t.service_id', $this->service_id);
    $criteria->compare('t.title', $this->title, true);
    $criteria->compare('t.comment', $this->comment, true);
    $criteria->compare('t.category_id', $this->category_id, true);
    $criteria->order = 't.id DESC';
    
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'pagination'=>array(
            'pageSize'=>50,
        ),
    ));
    

    【讨论】:

    • 你救了我的命,第二个选项有效,非常感谢你的帮助,我试过一起使用,但一般标准。
    • 很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    相关资源
    最近更新 更多