Relation Data

Hi guys,

i have two tables like


Listing

ID 

cat_ID

name

price 

etc..


Cat

ID 

name

etc..

right i am doing this in view to get the related data for cat_id from table cat … the question is there is simpler and nice way doint it in controller




<?php $cat = Cat:findOne($model->cat_ID); 

	   echo $cat->name;  ?>

thanks

I don’t understand the problem but I guess this will work fine…

In your controller action,


$model = // I assume you load the 'Listing' model here


$cat = Cat::model()->findByPk( $model->cat_ID );


$this->renderPartial( '_yourView', array(

    'cat' => $cat,

    // other variables...

));




In your view,




<?php


    echo $cat->name; 


?>

thank you buddy, i thought maybe there is smarter way … you way also superb …

Why not just add a join to the Listing query?

maybe you can give a code… i was going through documentation… didn’t get it…

It’s right there: https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md#joining-with-relations