Yii 1.1关系搜索问题

w1e3prcc  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(157)

1.表格meeting

型号名称CoreMeeting
字段id, title, start time, created_by
关系:

'MeetingParticipants' => array(self::HAS_MANY, 'MeetingParticipants', 'meeting_id'),

2.表格core_meeting_participant

型号名称为meeting_participant
字段为id, meeting_id, participant_id, group_id
关系:

'meeting' => array(self::BELONGS_TO, 'CoreMeeting', 'meeting_id'),
 'group' => array(self::BELONGS_TO, 'MeetingGroup', 'group_id'),

3.表格core_meeting_group

型号名称为MeetingGroup
字段为id, group_name
我在会议模型中的搜索筛选器是:

public function search()
{

    $group=filter_var($_REQUEST['group'], FILTER_SANITIZE_STRING);//contain group name

    $criteria=new CDbCriteria;
    $user_id = Yii::app()->user->id;

    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->title,true);
    $criteria->with=array('MeetingParticipants'=>array("select"=>"*"),'MeetingParticipants.group'=>array('select'=>'id,group_name'));

    if(isset($this->start_time) && !empty($this->start_time))
    $criteria->compare('start_time',date("Y-m-d", strtotime($this->start_time)), true);

    $criteria->compare('created_by',$user_id);

    if(isset($group)&&!empty($group))
    $criteria->compare('group.group_name',$group);
    $criteria->together = true;

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,

    ));
}

我已经创建了4个会议,每个会议至少有5个参与者,每个参与者都属于一个会议组.我希望列出所有会议,并在下面列出会议标题,组与会议时间.
我的问题是,如果启用$criteria->together = true;,会议的参与者超过10人,则在网格视图中仅显示1个会议。如果禁用此选项,则将显示所有会议,但无法使用会议名称进行搜索。SQL fiddle链接为http://sqlfiddle.com/#!9/fdaacf full SQL dump https://pastebin.com/NtpMuCpE

efzxgjgh

efzxgjgh1#

CDbCriteria-〉together属性的文档:
https://www.yiiframework.com/doc/api/1.1/CDbCriteria#together-detail
在单个SQL中是否应将外表与主表联接。此属性仅用于关系AR查询中的HAS_MANY和MANY_MANY关系。
正如我在CoreMeeting模型中看到的关系:

  • '组' =〉数组(自身::属于,'会议组','组标识')*

很明显,您正试图通过全局设置CDbCriteria-〉together = true来急切地获取一对一(BELONGS_TO)关系CoreMeeting-〉MeetingGroup;这根据文档是不正确的。
我的建议是,首先去掉这个全局together设置为CDbCriteria,然后尝试将with子句更改为:

$criteria->with = [
    // this is your has-many relation (one meeting has several participants), thus we set here `together` to true
    'MeetingParticipants' => [
        'together' => true,
        'with' => [
            // this is your one-to-one relation (one participant has one group)
            'group'
        ]
    ]
];

相关问题