Relational Query: Many Levels Of Belongs To

I have the following tables:

patient

  • id

  • first_name

  • last_name

event

  • id

  • patient_id

  • event_description

notification

  • id

  • event_id

  • notification_description

I have the following relations defined in my models:

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'),

		);

	}



I want to get all notifications for patient with first name "Joe" and last name "Smith". Is there a way to do that without writing out an SQL statement?

Look at relational query section

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

you can use criteria "with"

Thanks. Following what I did in my notification search method:




        $criteria=new CDbCriteria;

        $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');

        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));