0
votes

I am new to Yii framework. So i need help. I have 2 tables

table A

admin_id admin_name
1              aaa

2              sss

3              eee

table B

id admin_id phone_num

1   1            123123123

2   1            234234234

3   2            343434344

So when displaying table B i want to display admin_name from table A instead of admin_id

I have a relation defined in Table B model as

return array('admin' => array(self::BELONGS_TO, 'table A', 'admin_id'),);

I want to use relations concept to get the admin name.

One more important thing is I am not using views of Yii. So i want only controller and model to be used.

Kindly help me out with this issue.

2

2 Answers

2
votes

You can get the value with:

$model = TableB::model()->findBy....();
echo $model->admin->admin_name;

See http://www.yiiframework.com/doc/guide/1.1/en/database.arr for more details on lazy and eager loading. Btw: It's recommended not to prefix your column names.

0
votes

I displayed company category name inside company admin page using CGridView as under:

 $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'company-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    'id',
    'name',
    array( 
    'name'=>'company_category_id',
    'value'=>'$data->companyCategory->category_name',
    ),
    'uan',
    'website',
    array(
        'class'=>'CButtonColumn',
    ),
),

));