繁体   English   中英

Yii Model-> search()标准与MANY_MANY关系进行比较

[英]Yii Model->search() criteria compare with MANY_MANY relation

我有三张桌子:

在此输入图像描述

以及其中两个的模型有很多关系

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不存在,但我无法理解 - 为什么会发生这种情况。

尝试把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,
    ),
));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM