Yii Framework Forum: Display Relational Data... - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Display Relational Data... Rate Topic: -----

#1 User is offline   karasawaz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-August 12

Posted 21 November 2012 - 11:16 PM

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?
0

#2 User is offline   yiiplayboy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 03-May 12

Posted 21 November 2012 - 11:36 PM

View Postkarasawaz, on 21 November 2012 - 11:16 PM, said:

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....
0

#3 User is offline   karasawaz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-August 12

Posted 20 January 2013 - 10:15 PM

Quote

//This is the CDetailView code from view.php

<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'code',
'name',
'brand_id', //i want to show the name instead of id
'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 />


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

#4 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 21 January 2013 - 03:51 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users