2013-03-19 2 views
1

У меня есть следующие таблицы:Yii запрос через кратному относится к отношениям

пациент

  • ID
  • first_name
  • last_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'), 
    ); 
} 

Я хочу, чтобы все уведомления для пациента с именем «Джо» и las t имя «Смит». Есть ли способ сделать это, не выписывая инструкцию SQL?

+0

да, возможно, это как cgridview работает –

+0

, когда вы прыгаете от события к уведомлению, yu может понадобиться HAS_MANY –

ответ

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

+0

отлично, хорошая работа –

Смежные вопросы