Php Yii通过多个“属于”关系查询

Php Yii通过多个“属于”关系查询,php,yii,Php,Yii,我有以下表格: 患者 身份证 名字 姓 事件 身份证 病人编号 事件描述 通知 身份证 事件id 通知和说明 我的模型中定义了以下关系: Notification.php public function relations() { return array( 'event' => array(self::BELONGS_TO, 'Event', 'event_id'), ); } public function

我有以下表格:

患者

  • 身份证
  • 名字
事件

  • 身份证
  • 病人编号
  • 事件描述
通知

  • 身份证
  • 事件id
  • 通知和说明
我的模型中定义了以下关系:

Notification.php

public function relations()
{
    return array(
                'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            );
}
public function relations()
{
    return array(
                'patient' => array(self::BELONGS_TO, 'Patient', 'patient_id'),
    );
}
Event.php

public function relations()
{
    return array(
                'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            );
}
public function relations()
{
    return array(
                'patient' => array(self::BELONGS_TO, 'Patient', 'patient_id'),
    );
}

我想获得名为“Joe”姓为“Smith”的患者的所有通知。有没有一种不用写SQL语句就可以做到这一点的方法?

您需要一个自定义搜索函数,如:

    public function searchCandidates() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.

        $criteria = new CDbCriteria;

        $criteria->with = array('candidate_relation');//this is a relation; you can pute here, relation_a.relation_b.relation_c
        $criteria->compare('id', $this->id);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('password', $this->password);
        $criteria->compare('created', $this->created);
        $criteria->compare('lastmodified', $this->lastmodified);
        $criteria->compare('confirmed', $this->confirmed);
        $criteria->compare('is_candidate', 1);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('candidate_relation.first_name', $this->full_name, true);//and another relation here ...
        $criteria->compare('candidate_relation.last_name', $this->full_name, true, 'OR');

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

其中,
full\u name
是模型的自定义属性:
public$full\u name

是的,这是可能的,这就是cgridview的工作方式。当您从事件跳转到通知时,您可能需要有多个存储库。根据你的建议,我用了以下方法使它起作用$标准->带=数组('event.patient')$条件->比较('patient.first_name',$this->search_string,true'或')$条件->比较('patient.last_name',$this->search_string,true'或');