Display Relational Data...

Sry for being so straight but i’ve search some of the topic but i just don’t understand…

Let’s say i have 2 tables…

First is Brand with brand_id and brand_name as variable

table brand

brand_id (int)

brand_name (varchar)

the next table is product

table product

product_id (int)

product_brand (id), references to table brand (brand_id)

product_name (varchar)

but in the view.php when showing the product… instead of showing the product_brand (id), i want to show the product name

example

product_id: 114326

product_brand: Samsung (instead of the id, show the name)

product_name: Galaxy Note 10.1

how to do that?

In the ‘attributes’ array of your CDetailView, try this:

array( ‘name’=>‘prod_brand’, ‘value’=>$model->brand->brand_name),

in place of the ‘product_brand’ entry you must be having in your code…dont forget to define $prod_brand in your model else it throws undefined errors…

for it to work you must have your relations specified in your model…generate your model using gii and it would create the relations for you…

That’s the CDetailView in my view.php, could u specify the modified part of the view?

You can try:




//This is the CDetailView code from view.php


<?php $this->widget('zii.widgets.CDetailView', array(

'data'=>$model,

'attributes'=>array(

'id',

'code',

'name',

array( 'name'=>'prod_brand', 'value'=>$data->brand->product_name),

'category_id',

'description',

'image_path',

'price',

'active',

),

)); ?>




//And this is the code from _view.php


<b><?php echo CHtml::encode($data->getAttributeLabel('brand_id')); ?>:</b>

<?php echo CHtml::encode($data->brand_id); ?>

<br />



And brand should be specified as a relations in your model.