How To Display Field From Related Model

Hii Everybody…

please help me to discover solution for my error,

i wanna to display field from related model.

i have 4 tabel related, but i cant call field from other model,

i have tabel DetailTransaction and Transaction,

DetailTransaction (id_dt, trxid, status)

Transaction(id_trx, trxid, name)

in DetailTransaction model i added

'trx'=>array(self::BELONGS_TO,'Trx','trxid'),

in Transaction model i added,

    'dt'=>array(self::BELONGS_TO,'DetailTransaction','trxid'),

my question, how i can display status from DetailTransaction in model Transaction,

what step by step i need to do??

please give me solution or way to solve my problem,thankz a lot.

you can display related record using




$model=Transaction::model()->find(1);

echo $model->dt->status;



because


$model->dt

returns its related parent record object. and hence you can display the available columns using


$model->dt

you can display related record using




$model=Transaction::model()->find(1);

echo $model->dt->status;



because


$model->dt

returns its related parent record object. and hence you can display the available columns using


$model->dt

try this like





$model = PostCategory::model();

$record = $model->with(array(

  'posts'=>array(

     'order'=>'posts.createTime DESC',

  ))->findByPK($id,

             array('limit'=>3,'together'=>true) // adding this works

);

if you want to display admin grid view

1)You need to setup a relation to that table, and you can use the relation to reference that field like this:


$data->member->name;

2)In your model you would place something like:





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(

            'member' => array(self::BELONGS_TO, 'Member', 'member_id'),// foreign key

        );

    }

  1. and for grid you would do like:




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

'id'=>'transaction-grid',

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

'filter'=>$model,

'columns'=>array(

    'id',

       array(

            'header' => 'Member',

            'name' => 'member_id',

            'value' => '$data->member->name'

        ),


    'Location',

    ...

    array(

        'class'=>'CButtonColumn',

        'template'=>'{view}',

    ),

),

)); 



I hope it’s some help.