phpyiicgridviewzii-widgets

Yii zii.widgets.grid.CGridView custom field


I am new to yii. In my blocked-recruiter view's admin.php page i have a CGridView widget. the candidate_id is the foreign key of candidate table. So now in blocked recruiter view the candidate_id is coming by default. But i want to show the candidate name here, which is in the candidate table. to get the candidate name by candidate_id i have to use Candidate::model()->findByAttributes('id'=>$candidate_id)->name; But i am not able to use the code, basically i dont khow how do i use it.

Code in admin.php of blocked-recruiter view page.

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'blocked-recruiter-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'candidate_id',
        'recruiter_id',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

So, please help to to get the candidate_name here instate of candidate_id.

Thank You.


Solution

  • In your blocked-recruiter Model, BlockedRecruiter i presume, define a relation candidate like this:

    public function relations() {
        return array(
            'candidate'=>array(self::BELONGS_TO, 'Candidate', 'candidate_id'),
        );
    }
    

    The you can access the candidate's name in your GridView using candidate.name instead of candidate_id