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.
Patients
id_patient
name
lastname
Turns
id_turn
id_patient
time
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() ;
CVarDumper::dump($res)
;
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
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?
Seems like you finally ran out of patients
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.
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
http://www.yiiframework.com/forum/index.php/topic/4148-ar-relation-foreign-key/
Obrigado ORey
Bye.