Hi everyone,
This is the first framework I really look into and use. So far I’ve got most of my app running smoothly thanks to the documentation and threads on the forum but I’ve got stuck whilst trying to get data from multiple tables.
The site I’m working on is a car site, here’s a simplified example of the db tables I’m having trouble with:
lpa_vehicule :
id, marque, ...
lpa_marque :
id, nom_marque, ...
marque in the lpa_vehicule table refers to the PK id in the other table. I was reading into the relations() as I thought that was what I needed now I’m not so sure. I’ve got it so it’s sort of working however it always queries the vehicule id against the other tables, I’m trying to figure out how to use marque on the lpa_marque table.
Maybe I’ve completely misunderstood this part, this is what I’ve done so far with the relations:
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(
'marque_info' => array(self::HAS_ONE, 'Marque', 'id'),
);
}
In the controller:
$this->_model=Vehicule::model()->with('Marque')->findbyPk($_GET['id']);
I’ve tried playing around with the foreignKey, etc. but I can’t work out how to change the actual key used from the primary table. Basically the end of the query looks like this:
LEFT OUTER JOIN `lpa_marque` `Marque` ON (`Marque`.`id`=`t`.`id`)
And I’m trying to acheive this:
LEFT OUTER JOIN `lpa_marque` `Marque` ON (`Marque`.`id`=`t`.`marque`)
I would really appreciate any help with this, maybe I’m trying to do this the wrong way if so please point me in the right direction
I’ve already read this page 10 times: http://www.yiiframework.com/doc/guide/database.arr
Thanks in advance!