CGridView and accessing data via relations

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?

try this:




$this->widget('zii.widgets.grid.CGridView', array(

        ...

        'columns'=>array(

                ...

                array(

                    'name' => 'qCategoryKey',

                    'value' => '$data->categoryLink->categoryName'

                ),

                ...

        ),

));



it now says: "Undefined variable: model".

Is there some sort of switch to activate the relations side of AR? I am working on the generated code of gii.

//Edit: Ahh sorry you said $data. It is working this way and I have to say I am very pissed if it was just the quotes :)

Thanks for the help!

mostly the quote because without quotes this expression is evaluated when component is configured (once at startup). With quotes - it is an expression evaluated for every record. But besides of quotes you have to also use $data variable, not $model (like you noticed) :)

Could you not just do:


$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'q-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array(

                'questionId',

                'categoryLink.categoryName',

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

));

It does not work that way. says “Use of undefined constant kategori - assumed ‘kategori’”.

I have a new problem now. Everything is working in my development server. But when i push all the files to the production server, the relations variables ($data->categoryLink->categoryName) does not work. It does not display anything in the grid.

Do you guys have any idea why this might happen? The files (both the framework and the project) are identical.

There is a difference in the DB though. I did not setup foreign keys in the production database. But as far as I’m concerned, the framework does not need them to setup relations via code.

Apparently it works when I add the foreign keys in the database. But I am 100% sure I’ve read somewhere in the wiki that it does not need the hard FK relations.

Probably a very late addition to this thread, but in case someone stumbles upon it again…

It does not require the actual Foreign Key relationship between the Parent and Child table however it does require the Primary Key on the table it is referring to from child table. I don’t know which version you were using, this is what I have tested just now on Yii 1.1.12 by removing the Foreign Key first and then the primary key. As soon as I dropped the PK, it threw error.

Some thing that Yii Dev team to comment on and also mention as to why Yii depends on constraints defined on DB level when the relationships are defined at model level ?