Help With Relational Querys And Cactiverecord

Hello, I’m building a website with yii for the first time in my workplace, so i’m learning and doing at the same time.

I’m trying to use the with function of CActiveRecord and I just can get a breakthrough while trying to do SQL Joins, this is what I have simplified.









The relation is 1 turn has 1 patient

So in my Turns Model (generated with gii) I have this

	public function relations()


		return array(

			'patients'=>array(self::HAS_ONE, 'Patients', 'id_patient'),



But when I run something like

$res = Turns::model()->with("patients")->findAll() ; 



I always get a null value in the patients field of each record

Am I missing something? Thanks in advance.

Seems like you finally ran out of patients ;D

Ok, joking aside. First try to run this query in DB console / DB explorer / phpmyadmin etc, and see i there are any results.

Maybe you just don’t have related records?

PS. You can also see the query generated by Yii in app logs.

OMG good tip I just enabled the db log and it’s trying to compare patient_id with turn_id, what can I change so that it uses the foreign key instead of the primary key?

I got it!

I have to use this relation

'patients'=>array(self::HAS_ONE, 'Patients', '', 'on'=>'patients.id_patient=turns.id_patient'),

Found that in this post

Obrigado ORey