Newb Yii/ar Relation Problem

Hello all,

Obvious Yii, and AR in general, newb here. I’m Playing with a basic pc inventory project to get my feet wet and having what is hopefully an obvious hiccup to the more advanced fora goer. I’ve followed Web Application Development with Yii and PHP in its entirety, studied the docs/guides, and searched for related problems but still can’t seem to track this one down. So without boring anyone further here’s my basic problem.

Here’s my simplified schema with relevant columns

[indent]

pcmodel table

[indent]id (PK)

description

ramtype_id (FK: ramtype.id)

[/indent]

ramtype table

[indent]id (PK)

description[/indent]

machine table

[indent]id (PK)

pcmodel_id (FK: pcmodel.id)[/indent]

[/indent]

Relations




//in model Pcmodel::relations()

return array(

     //A PC model can have any number of machines

     'machines' => array(self::HAS_MANY, 'Machine', 'pcmodel_id'),

     //A PC model can only have one ramtype

     'ramtype' => array(self::HAS_ONE, 'Ramtype', 'ramtype_id'),

);


//in model Ramtype::relations()

return array(

     //A ramtype belongs to many PC models

     'pcmodels' => array(self::BELONGS_TO, 'Pcmodel', 'ramtype_id'),

);


//in model Machine::relations()

return array(

     //A machine can only be one PC model

     'pcmodel' => array(self::BELONGS_TO, 'Pcmodel', 'pcmodel_id'),

);



actionIndex in both PcmodelController and MachineController are the vanilla Gii generated actions which then render the default CListView with ‘itemView’=>’_view’




//in PcmodelController.php

public function actionIndex()

{

     $dataProvider=new CActiveDataProvider('Pcmodel');	

     $this->render('index',array(

          'dataProvider'=>$dataProvider,

     ));

}


//in MachineController.php

public function actionIndex()

{

     $dataProvider=new CActiveDataProvider('Machine');

     $this->render('index',array(

          'dataProvider'=>$dataProvider,

     ));

}



The relations seem to be fine for machine -> pcmodel, for instance in views/machine/_view.php I can do $data->pcmodel->description just fine. However when I try to do $data->ramtype->description in views/pcmodel/_view.php I get the following CDbException:

Clearly the problem is the resulting SQL is using an incorrect column (ramtype.ramtype_id should be ramtype.id) in the where clause, I just can’t for the life of me figure out why. I’ve tried to keep this as brief as possible but if I’ve left anything out I’m happy to include whatever might be helpful in clearing this up. Thanks for reading and, again, I greatly appreciate any insight.


'ramtype' => array(self::HAS_ONE, 'Ramtype', 'ramtype_id'),

should be


'ramtype' => array(self::BELONGS_TO, 'Ramtype', 'ramtype_id'),

as in Machine::relations().

Ramtype on the other hand shoudl use HAS_ONE instead of BELONGS_TO.

Check RaR section of docs for more information about how the relations are declared.

Doh! That was it. I even read that BELONGS_TO vs HAS_ONE doc and still couldn’t put it together. Time for more studying! Thanks for helping a clearly Yii challenged person ORey.