从 yii1 中有外键的 table 获取数据
fetch data from a table having foreign key in yii1
我有一个 table 名字 business
和第二个 table address
。商家 table 具有属性,id,buisness name,image
,地址 table 具有属性 address_id,business_id,street,city,house
。
在我的商业模式中,我有这样的关系 'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
。
我想要的是从 address
table 中获取像 street,city,house
这样的数据,并在 Business
的 Cdetail 视图中显示。我只知道这跟关系是可以做到的,但不知道确切的答案。
下面是我的 Business
.
的 Cdetail 视图
<?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?
从您的数据库架构中我可以看出一个企业可以有多个地址。
如果你想显示所有这些,你可以使用以下内容:
<?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,
)); ?>
好的,这是交易,我有两个 tables,business
和 address
,地址 table 有 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">0</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. 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">5</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">5</span>
</div>-->
</div> </a>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="gap gap-small"></div>
我也post这个答案,因为如果你想为每个地址设置不同的行,我以前的答案是有效的。
这一个使用您要求的关系在一行中显示所有地址的完整地址(城市、街道、房屋)。我已经检查过了,它可以正常工作。
首先,在您的 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,
));
}
最后,在你的 business/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'
),
),
)); ?>
我有一个 table 名字 business
和第二个 table address
。商家 table 具有属性,id,buisness name,image
,地址 table 具有属性 address_id,business_id,street,city,house
。
在我的商业模式中,我有这样的关系 'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
。
我想要的是从 address
table 中获取像 street,city,house
这样的数据,并在 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?
从您的数据库架构中我可以看出一个企业可以有多个地址。 如果你想显示所有这些,你可以使用以下内容:
<?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,
)); ?>
好的,这是交易,我有两个 tables,business
和 address
,地址 table 有 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">0</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. 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">5</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">5</span>
</div>-->
</div> </a>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="gap gap-small"></div>
我也post这个答案,因为如果你想为每个地址设置不同的行,我以前的答案是有效的。
这一个使用您要求的关系在一行中显示所有地址的完整地址(城市、街道、房屋)。我已经检查过了,它可以正常工作。
首先,在您的 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,
));
}
最后,在你的 business/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'
),
),
)); ?>