Hi everyone.
I am trying to implement a small admin panel to a web application. But I’ve bumped onto a simple question.
I have 2 tables, structured as shown below:
Category
categoryId | categoryKey | categoryName
Question
questionId | qCategoryKey | question
The tables are linked via the categoryKey columns.
In the Question Model:
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(
'categoryLink' => array(self::BELONGS_TO, 'Category', 'qCategoryKey'),
);
}
In the Category Model:
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(
'questions' => array(self::HAS_MANY, 'Question', 'qCategoryKey'),
);
}
In the admin.php page of Question Model (generated by CRUD), I have the CGridView. I am trying to show the name of the category of the questions in the table.
My CGridView code is as follows:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'q-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'questionId',
array(
'name' => 'qCategoryKey',
'value' => $model->categoryLink->categoryName
),
array(
'class'=>'CButtonColumn',
),
),
));
And it says “Trying to get property of non-object”, it does not recognize the link “categoryLink” between the models. I’ve searched this forum and many other places and everyone seems to access this way. But it does not work for me and I could not understand why. I also cant seem to access any $data.
How am i gonna make the categoryName appear in the table rather than the key?