0
votes

I have two table Event and EventCategory. I am using zii.widgets.grid.CGridView widget to display all the events in the admin section.

I have created the following relations between tables.

Relation in EventCategory model:

'event' => array(self::HAS_MANY, 'Event', 'category_id'),

Relation in Event model:

'category' => array(self::BELONGS_TO, 'EventCategory', 'category_id'),

The following code I am using to display the Events:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'event-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'summaryText'=> '',
    'columns'=>array(
         array('header'=>'Id','name'=>'event_id','filter'=>''),
         'event_code',
         'category.evntcatm_name',
         'event_name',
         array(
             'class'=>'CButtonColumn',
             'htmlOptions'=>array('class'=>'actions aligncenter'),
             'deleteButtonImageUrl'=>false,
             'updateButtonImageUrl'=>false,
             'viewButtonImageUrl'=>false
          ),
    ),
)); ?>

'category.evntcatm_name' doesn't display anything. It is just creating the blank column with NO ERROR. What I am missing here.

5
Can you double check if the column is actually filled in the db - MrSoundless
Yes. Data is there, but its not displaying anything. - Mohit Bhansali
Do you have your relation set up in your model? - MrSoundless
Yes. I have done that. I have added the relations in the question description. Let me know, if you find any mistake there. - Mohit Bhansali
Also double check your foreign keys. I believe relations in Yii don't work without foreign keys. - MrSoundless

5 Answers

1
votes

You should use code like this:

'columns' = array(
    /*YOUR DATA*/
    array(      
        'name' => 'category_id', 
        'value' => function($data) {
           return !empty($data->category) ? $data->category->evntcatm_name : null;
    }),
)

This way can solve your problem with "Trying to get property of non-object". If you see blank cell in grid that means you miss relation (or it doesnt exist, in this way you should check relation condition or database)

3
votes

Please try something like it

$data->category->evntcatm_name
0
votes

Here is a few wild guesses:

  • Is category.evntcatm_name correctly spelled?
  • Is there actually data in the evntcatm_name field to begin with?

I know it might sound too simple to miss, but the error almost has to be on that level. Try finding a category using the primary key and output it's evntcatm_name.

$cat = EventCategory::model()->findByPk(1);
echo $cat->evntcatm_name;

Maybe if you could share your schema for those two tables?

0
votes

You can use

    array(
              'header' => 'Category Title',
              'value' => '$data->category->evntcatm_name'
    ),

instead of 'category.evntcatm_name'

-1
votes

The model instance is not object now in the Cgidview: Try

$data['category']['evntcatm_name'];