我有以下表格:
患者
事件
通知
我的模型中定义了以下关系:
Notification.php
public function relations()
{
return array(
'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
);
}
Event.php
public function relations()
{
return array(
'patient' => array(self::BELONGS_TO, 'Patient', 'patient_id'),
);
}
我希望获得姓名为“Joe”且姓氏为“Smith”的患者的所有通知。有没有办法在不写出SQL语句的情况下做到这一点?
答案 0 :(得分:2)
您需要自定义搜索功能,例如:
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_name
是模型的自定义属性:public $full_name;