Yii Framework Forum: Relational Query: Many Levels Of Belongs To - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relational Query: Many Levels Of Belongs To Rate Topic: -----

#1 User is offline   Stephen51 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 09-May 12

Posted 19 March 2013 - 12:30 PM

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?
0

#2 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 19 March 2013 - 01:58 PM

Look at relational query section
http://www.yiiframew...en/database.arr
you can use criteria "with"
1

#3 User is offline   Stephen51 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 09-May 12

Posted 19 March 2013 - 02:15 PM

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

1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users