Relating Tables

Hi Yii Forum.

I had two tables by name appointment and patient. I had a column status in appointment table. If the status is approved I need to add some fields of appointment table to patient table… Can any one say me how to do dis…

Thanks in advance…

Did you find a solution to this.

I think the best way is to have the fields already created in the patient table.

Then when the appointment is confirmed, you can then populate them.

What do you think? :unsure:

If I understand this correctly, a patient entry would have many appointment entries correct? If so, you can just stick to relations and not have to worry about moving data from 1 table to another related table.

Inside Patient Model




// Relations for the Patient model 

public function relations()

{

	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'appointments' => array(self::HAS_MANY, 'Appointment', 'patient_id'),

	);

}



Inside the Appointment Model




// Relations for the Appointment model 

public function relations()

{

	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'patient' => array(self::BELONGS_TO, 'Patient', 'patient_id'),

	);

}


public function scopes()

{

	return array(

		'getApproved' => array(

			'condition' => 'approved_appointment = 1',

		),

	);

}



In a controller




public function actionViewPatientAppointments($id)

{

	// Grab patient

	$patient = $this->loadModel($id);

	

	// Get all approved appointments

	$appointments = $patient->appointments(array(

		'scopes' => array(

			'getApproved',

		),

	));

	

	// Loop through all approved appointments

	foreach ($appointments as $appointment)

	{

		// Do what you want with the $appointment data here

	}

}



That’s just something I put together to pull all approved appointments for a patient. The table fields probably do not match so just edit them as needed.