As you would guess from the two snips below, I will reference the Category table when displaying Part details.
Part model:
public function relations() {
return array(
'category' => array(self::BELONGS_TO, 'Category', 'fid_caty2') );
}
Category model:
public function relations() {
return array('part' => array(self::HAS_MANY, 'Part', 'fid_caty2') );
}
I've learned this method of rendering the referenced value in a view:
echo CHtml::encode($data->category->name_caty2);
I've found some other methods too, but they all take place in the view. Since the model sets the relationship, why shouldn't the referenced value be included in the CActiveRecord
of the model? I thought this was exactly the kind of crunching we reserved for the model. Working it out in a view seems strange, plus a lot of extra work.
If I'm right, how do I make it work in the model view? Or does my understanding of MVC need correction?
How about this
public function loadModel($id)
{ $model=Part::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
Here are in the controller page. Can I sneak my referenced value into the model when it is loaded?