1

我有以下表格:

病人

  • ID

事件

  • ID
  • 患者编号
  • 事件描述

通知

  • ID
  • event_id
  • 通知描述

我的模型中定义了以下关系:

通知.php

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

事件.php

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

我想获得名字为“Joe”和姓氏为“Smith”的患者的所有通知。有没有办法在不写出 SQL 语句的情况下做到这一点?

4

1 回答 1

2

you need a custom search function, like:

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

where full_name is a custom property for the model: public $full_name;

于 2013-03-19T18:24:56.737 回答