简体   繁体   中英

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

I have three tables:

在此输入图像描述

and the models for two of it with relation MANY to 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)' ),
);

And I am trying to make grid filtration using dropdowns with tbl_social_message_types.id for compare.

My search:

$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,
    ),
));

but when I make a request (changing filter field), Yii returns me DB error:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mtypes.id' in 'where clause'. The SQL statement executed was:

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

I understand, that table mtypes does not exists, but I can't understand - why this happening.

Try to place with criteria first:

$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,
    ),
));

If it won't work, sometimes Yii requires 'together' => true parameter.

$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,
    ),
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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