2013-03-19 63 views
1

我有以下表格:Yii的查詢所屬關係

患者

  • ID
  • FIRST_NAME
  • 姓氏

事件

  • ID
  • patient_id
  • EVENT_DESCRIPTION

通知

  • ID
  • EVENT_ID
  • notification_descript離子

我在我的模型定義如下關係:

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」和las的患者t名字「史密斯」。有沒有辦法做到這一點,而不寫出一條SQL語句?

+0

是的,這是可能的,這就是cgridview如何工作 – 2013-03-19 18:19:44

+0

當你從事件跳到要通知,yu可能需要HAS_MANY – 2013-03-19 18:27:00

回答

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;

+0

謝謝。根據您的建議,我使用以下內容來實現它。 $ criteria-> with = array('event.patient'); $ criteria-> compare('patient.first_name',$ this-> search_string,true,'OR'); $ criteria-> compare('patient.last_name',$ this-> search_string,true,'OR'); – Stephen305 2013-03-19 18:30:28

+0

完美,好工作 – 2013-03-19 20:14:59