Undefined Variable $data in CActiveDataProvider - Yii Framework

1k views Asked by At

I want to fetch data from my table using CActiveDataProvider in Yii. Everything seems to be working well but when I want to display the data from another related table using relations, I get an error. 'Undefined variable $data'.

here is my admin.php view:

<h1>Manage Teams</h1>


<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'team-grid',
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'id',
        'team_name',
        array(
            'name'=>'league_id',
            'type'=>'raw',
            'value'=>$data->league->league_name,
            ),
        'create_time',
        'update_time',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

and here is my actionAdmin method on TeamController.php

public function actionAdmin()
    {
        $dataProvider=new CActiveDataProvider('Team', array(

            'criteria'=>array(
                'order'=>'create_time DESC',
            ),
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));

        $this->render('admin',array(
            'dataProvider'=>$dataProvider,
        ));
    }

The relation is, obviously, a team belongs to a league.

2

There are 2 answers

0
Moneer Kamal On BEST ANSWER

you need to do it like this :

your column should be

array(
        'name'=>'league_id',

        'value'=>array($this,'league_name'),
        ),

and you controller shod have a function like this:

public function league_name($data,$row)
{

    return $data->league->league_name;

}
0
robertgliguroski On

You should just put the $data->league->league_name in quotes and then it will recognize the $data variable. It should look like this:

array(
        'name'=>'league_id',
        'type'=>'raw',
        'value'=>'$data->league->league_name',
        ),