получить данные из таблицы с внешним ключом в yii1

У меня есть имя таблицы business и второй стол address, Бизнес-таблица имеет атрибуты, id,buisness name,image и адресная таблица имеет атрибутыaddress_id,business_id,street,city,house,

В моей бизнес-модели у меня такие отношения 'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),,

То, что я хочу, это получить данные как street,city,house от address таблица и показать, что в представлении Cdetail Business, Я просто знаю, что это можно сделать с помощью отношений, но не знаю точно ответа.
Ниже мой Cdetail вид 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',
),
)); ?>

после долгих поисков
я наконец сделал это

<?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'
)


),
)); ?>

теперь я получаю название города в cdetailview, но как я могу получить сектор, street_number тоже?

2

Решение

Из вашей схемы базы данных я вижу, что у бизнеса может быть много адресов.
Если вы хотите отобразить все из них, вы можете использовать следующее:

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

//Create the attributes array to use it in CDetailView
$attributes = array(
'id',
'business_name',
'image',
);

//Get all the addresses for the specific business_id
$addresses = Address::model()->findAll(
array(
'select'=>'city', 'condition'=>'business_id=:business_id',
'params'=>array(':business_id'=>$model->id)));

//Insert all the addresses in your attributes array
$i = 1;
foreach($addresses as $key => $value) {
$attributes[] = array(
'name'=>'City '.$i++,
'value'=>$value->city
);
}
?>

<?php $this->widget('zii.widgets.CDetailView',array(
'htmlOptions' => array(
'class' => 'table table-striped table-condensed table-hover',
),
'data'=>$model,
'attributes'=>$attributes,
)); ?>
0

Другие решения

Я также публикую этот ответ, потому что мой предыдущий работает, если вы хотите иметь разные строки для каждого адреса.

Этот показывает полный адрес (город, улица, дом) в одной строке для всех адресов, используя отношения, как вы просили. Я проверил это, и он работает правильно.

Сначала объявите виртуальный атрибут на вашем Addresses модель.

public $address;

Затем в BusinessController заменить actionView со следующим:

public function actionView($id) {
$model = Business::model()->with(array(
'addresses'=>array(
'select'=>(['alias'=>'CONCAT(city, " ", street, " ", house) AS address'])
)
))->find(array('condition'=>'id=:id', 'params'=>array(':id'=>$id)));


$this->render('view', array(
'model'=>$model,
));
}

Наконец, в вашем бизнесе / view.php напишите:

<?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',
'value' => function ($data) {
//Variable $addresses contains all addresses as a comma separated list
$addresses = CHtml::listData($data->addresses, 'address_id', 'address');
return implode(', ', $addresses);
},
'type'=>'raw'
),
),
)); ?>
0

Хорошо, вот сделка, у меня есть две таблицы, business а также addressадресная таблица имеет business_id ключ как внешний ключ, и я хочу показать, business address в одном из представлений бизнеса я сделал, я называю это views/(userbusiness.php),
Я скопировал вставить этот код из моего views/view.php в views/userbusiness.php,

<?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')),
);
?>

после этого я получаю данные о бизнесе address просто так в userbusiness.php,

<?php $address= $model->addresses;
foreach ($address as $ad)
{

echo $ad->street_number;
echo $ad->sector;
echo $ad->city;
}
?>

сейчас в моем userbusiness.php вид, я могу повторить street_number и т. д., где я хочу. так же, как ниже

<div class="booking-item-details">
<header class="booking-item-header">
<div class="row">
<div class="col-md-9">
<h2 class="lh1em"><?php echo $model->business_name; ?></h2>
<p class="lh1em text-small"><i class="fa fa-map-marker"></i> <?php $address= $model->addresses;
foreach ($address as $ad)
{

echo $ad->street_number;
echo $ad->sector;
echo $ad->city;
}
?></p>
<ul class="list list-inline text-small">

<li><a href="http://themonal.com/"><i class="fa fa-home"></i> Restaurant Website</a>
</li>
<li><i class="fa fa-phone"></i>+92-51-2898044s</li>
</ul>
</div>
<!--<div class="col-md-3">
<p class="booking-item-header-price"><small>price from</small>  <span class="text-lg">$350</span>/night</p>
</div>-->
</div>
</header>
<div class="row">
<div class="col-md-6">
<div class="tabbable booking-details-tabbable">
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#tab-1" data-toggle="tab"><i class="fa fa-camera"></i>Photos</a>
</li>

</ul>
<div class="tab-content">
<div class="tab-pane fade in active" id="tab-1">
<div class="fotorama" data-allowfullscreen="true" data-nav="thumbs">
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
<img src="<?php echo Yii::app()->request->baseUrl;?>/img/<?php echo $model->image; ?>" alt="Image Alternative text" title="hotel PORTO BAY SERRA GOLF living room" />
</div>
</div>

</div>
</div>
</div>
<div class="col-md-6">
<div class="booking-item-meta">
<h2 class="lh1em mt40">Outstanding!</h2>
<h3>97% <small >of guests recommend</small></h3>

<div class="booking-item-rating">
<ul class="icon-list icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
</ul><span class="booking-item-rating-number"><b >4.7</b> of 5 <small class="text-smaller"> rating</small></span>
<p><a class="text-default" href="#">based on 1535 reviews</a>
</p>
</div>
</div>
<div class="row">
<div class="col-md-12"> <!-- made it to 12 -->
<h4 class="lh1em">Business Rating Details</h4>
<ul class="list booking-item-raiting-list">
<li>
<div class="booking-item-raiting-list-title">Exellent</div>
<div class="booking-item-raiting-list-bar">
<div style="width:91%;"></div>
</div>
<div class="booking-item-raiting-list-number">1223</div>
</li>
<li>
<div class="booking-item-raiting-list-title">Very Good</div>
<div class="booking-item-raiting-list-bar">
<div style="width:6%;"></div>
</div>
<div class="booking-item-raiting-list-number">61</div>
</li>
<li>
<div class="booking-item-raiting-list-title">Average</div>
<div class="booking-item-raiting-list-bar">
<div style="width:5%;"></div>
</div>
<div class="booking-item-raiting-list-number">40</div>
</li>
<li>
<div class="booking-item-raiting-list-title">Poor</div>
<div class="booking-item-raiting-list-bar">
<div style="width:3%;"></div>
</div>
<div class="booking-item-raiting-list-number">15</div>
</li>
<li>
<div class="booking-item-raiting-list-title">Terrible</div>
<div class="booking-item-raiting-list-bar">
<div style="width:1%;"></div>
</div>
<div class="booking-item-raiting-list-number">9</div>
</li>
</ul>
</div>

</div>
<a href="themonal-review.php" class="btn btn-primary">Write a Review</a>
<a href="addphotos.php" class="btn btn-primary">Add Photos</a>
<a href="product-themonal-view.php" class="btn btn-primary">View Products</a>
<a href="claim-business.php" class="btn btn-primary">Claim This business</a>
</div>
</div>
<div class="gap"></div>
<h3>Reviews</h3>
<div class="row">
<div class="col-md-9">
<ul class="booking-item-reviews list">
<li>
<div class="row">
<div class="col-md-2">
<div class="booking-item-review-person">
<a class="booking-item-review-person-avatar round" href="user1profile.php">
<img src="img/salik.jpg" alt="Image Alternative text" title="Afro" />
</a>
<p class="booking-item-review-person-name"><a href="user1profile.php">Salik Asad</a>
</p>
<p class="booking-item-review-person-loc">Islamabad</p>
</div>
</div>
<div class="col-md-10">
<div class="booking-item-review-content">

<ul class="icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
</ul>
<p>Really liked this place. </p>
<p class="booking-item-review-rate">Was this review helpful?
<a class="fa fa-thumbs-o-up box-icon-inline round" href="#"></a><b class="text-color"> 7</b>
</p>
<!-- flag and trash -->
<div class="row">
<div class="col-md-12">
<ul class="icon-group booking-item-rating-stars">

<!-- <div class="col-md-1 col-md-offset-10">
<li><a href="#"><i class="fa fa-trash-o box-icon-inline round"></i></a></li></div> -->
<div class="col-md-1 col-md-offset-11">
<li><a href="flag-review.php"><i class="fa fa-flag-o box-icon-inline round"></i></a></li></div>
</ul>
</div>
</div>

</div>
</div>
</div>
</li>
<li>
<div class="row">
<div class="col-md-2">
<div class="booking-item-review-person">
<a class="booking-item-review-person-avatar round" href="user2profile.php">
<img src="img/waqas.jpg" alt="Image Alternative text" title="Bubbles" />
</a>
<p class="booking-item-review-person-name"><a href="user2profile.php">Muhammad Waqas</a>
</p>
<p class="booking-item-review-person-loc">Islamabad</p>


</div>
</div>
<div class="col-md-10">
<div class="booking-item-review-content">

<ul class="icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
</ul>
<p>Five out of five !!!!!! </p>
<p class="booking-item-review-rate">Was this review helpful?
<a class="fa fa-thumbs-o-up box-icon-inline round" href="#"></a><b class="text-color"> 7</b>
</p>
<!-- flag and trash -->
<div class="row">
<div class="col-md-12">
<ul class="icon-group booking-item-rating-stars">

<!-- <div class="col-md-1 col-md-offset-10">
<li><a href="#"><i class="fa fa-trash-o box-icon-inline round"></i></a></li></div> -->
<div class="col-md-1 col-md-offset-11">
<li><a href="flag-review.php"><i class="fa fa-flag-o box-icon-inline round"></i></a></li></div>
</ul>
</div>
</div>

</div>
</div>
</div>
</li>
<li>
<div class="row">
<div class="col-md-2">
<div class="booking-item-review-person">
<a class="booking-item-review-person-avatar round" href="user3profile.php">
<img src="img/nouman.jpg" alt="Image Alternative text" title="Good job" />
</a>
<p class="booking-item-review-person-name"><a href="user3profile.php">Nouman Ali Sethi</a>
</p>
<p class="booking-item-review-person-loc">Islamabad</p>
</div>
</div>
<div class="col-md-10">
<div class="booking-item-review-content">

<ul class="icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
</ul>
<p>Good Experience :) </p>
<p class="booking-item-review-rate">Was this review helpful?
<a class="fa fa-thumbs-o-up box-icon-inline round" href="#"></a><b class="text-color"> 7</b>
</p>
<!-- flag and trash -->
<div class="row">
<div class="col-md-12">
<ul class="icon-group booking-item-rating-stars">

<!-- <div class="col-md-1 col-md-offset-10">
<li><a href="#"><i class="fa fa-trash-o box-icon-inline round"></i></a></li></div> -->
<div class="col-md-1 col-md-offset-11">
<li><a href="flag-review.php"><i class="fa fa-flag-o box-icon-inline round"></i></a></li></div>
</ul>
</div>
</div>


</div>
</div>
</div>

</li>
</ul>
<div class="row wrap">
<div class="col-md-5">
<p><small>1108 reviews on this hotel. &nbsp;&nbsp;Showing 1 to 7</small>
</p>
</div>
<div class="col-md-7">
<ul class="pagination">
<li class="active"><a href="#">1</a>
</li>
<li><a href="#">2</a>
</li>
<li><a href="#">3</a>
</li>
<li><a href="#">4</a>
</li>
<li><a href="#">5</a>
</li>
<li><a href="#">6</a>
</li>
<li><a href="#">7</a>
</li>
<li class="dots">...</li>
<li><a href="#">43</a>
</li>
<li class="next"><a href="#">Next Page</a>
</li>
</ul>
</div>
</div>
<div class="gap gap-small"></div>
<div class="box bg-gray">
<h3>Write a Review</h3> <!------------------------------------------- -->
<form>
<div class="row">
<div class="col-md-12">


<div class="booking-item-rating">
<ul class="icon-list icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
</ul>

</div>
<div class="form-group">
<label>Review Text</label>
<textarea class="form-control" rows="6"></textarea>
</div>
<input class="btn btn-primary" type="submit" value="Leave a Review" />
</div>

</div>
</form>
</div>
</div>

<div class="col-md-3">
<h4>About the Business</h4>
<p class="mb30"> <?php echo $model->business_description; ?></p>
<table class="table">
<thead>
<tr>
<th>Hours</th>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody>
<tr>
<td>Mon</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr>
<tr>
<td>Tue</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr>
<tr>
<td>Wed</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr>
<tr>
<td>Thu</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr>
<tr>
<td>Mon</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr><tr>
<td>Fri</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr>
<tr>
<td>Sat</td>
<td>9:00am</td>
<td>11:00pm</td>
</tr>

</tbody>
</table>
<h4>Facilities Available</h4>
<ul class="booking-item-features booking-item-features-expand mb30 clearfix">
<li><i class="im im-wi-fi"></i><span class="booking-item-feature-title">Wi-Fi Internet</span>
</li>
<li><i class="im im-parking"></i><span class="booking-item-feature-title">Parking</span>
</li>
</ul>
<h4>You may like</h4>
<ul class="booking-list">
<li>
<div class="booking-item booking-item-small">
<a href="nandos.php">
<div class="row">
<div class="col-xs-4">
<img src="img/nandos.jpg" alt="Image Alternative text" title="Nandos" />
</div>
<div class="col-xs-5">
<h5 class="booking-item-title">Nandos</h5>
<ul class="icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
</ul>
</div>
<!--<div class="col-xs-3"><span class="booking-item-price-from">from</span><span class="booking-item-price">$345</span>
</div>-->
</div>
</a>
</div>
</li>
<li>
<div class="booking-item booking-item-small">

<a href="theemperorstable.php">
<div class="row">
<div class="col-xs-4">
<img src="img/emperor.jpg" alt="Image Alternative text" title="The Emperor's Table" />
</div>
<div class="col-xs-5">
<h5 class="booking-item-title">The Emperor's Table</h5>
<ul class="icon-group booking-item-rating-stars">
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star"></i>
</li>
<li><i class="fa fa-star-half-empty"></i>
</li>
</ul>
</div>
<!--<div class="col-xs-3"><span class="booking-item-price-from">from</span><span class="booking-item-price">$345</span>
</div>-->
</div> </a>
</div>
</li>


</ul>

</div>
</div>
</div>
</div>
<div class="gap gap-small"></div>
0
По вопросам рекламы [email protected]