I have a table name business
and second table address
. Business table has attributes, id,buisness name,image
and address table has attributesaddress_id,business_id,street,city,house
.
In my business model i have the relationship like this 'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
.
The thing which i want is to fetch the data like street,city,house
from the address
table and show that in the Cdetail view of Business
. I just know that it can be done with the relationship, but do not know exactly the answer.
Below is my Cdetail view of Business
.
<?php
/* @var $this BusinessController */
/* @var $model Business */
?>
<?php
$this->breadcrumbs=array(
'Businesses'=>array('index'),
$model->id,
);
$this->menu=array(
array('icon' => 'glyphicon glyphicon-list','label'=>'List Business', 'url'=>array('index')),
array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create Business', 'url'=>array('create')),
array('icon' => 'glyphicon glyphicon-edit','label'=>'Update Business', 'url'=>array('update', 'id'=>$model->id)),
array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete Business', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage Business', 'url'=>array('admin')),
);
?>
<?php echo BsHtml::pageHeader('View','Business '.$model->id) ?>
<?php $this->widget('zii.widgets.CDetailView',array(
'htmlOptions' => array(
'class' => 'table table-striped table-condensed table-hover',
),
'data'=>$model,
'attributes'=>array(
'id',
'business_name',
'image',
),
)); ?>
after a lot of googling i finally did this
<?php
/* @var $this BusinessController */
/* @var $model Business */
?>
<?php
$this->breadcrumbs=array(
'Businesses'=>array('index'),
$model->id,
);
$this->menu=array(
array('icon' => 'glyphicon glyphicon-list','label'=>'List Business', 'url'=>array('index')),
array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create Business', 'url'=>array('create')),
array('icon' => 'glyphicon glyphicon-edit','label'=>'Update Business', 'url'=>array('update', 'id'=>$model->id)),
array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete Business', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage Business', 'url'=>array('admin')),
);
?>
<?php echo BsHtml::pageHeader('View','Business '.$model->id) ?>
<?php $this->widget('zii.widgets.CDetailView',array(
'htmlOptions' => array(
'class' => 'table table-striped table-condensed table-hover',
),
'data'=>$model,
'attributes'=>array(
'id',
'business_name',
'image',
array(
'name' => 'addresses', //name of relation in business model
'value' => function ($data) {
// Get all related address using the relation defined in the business model and use CHtml::listData to store data inside the $addresses variable as an array using `id` as key and `child_name` as value
$addresses = CHtml::listData($data->addresses, 'id','sector');
// Return names as a comma separated list
return implode(', ', $addresses);
},
'type'=>'raw'
)
),
)); ?>
now i am getting city name in cdetailview but how can i get sector,street_number too ?