从yii1中具有外键的表中获取数据

时间:2015-10-18 16:54:54

标签: php yii foreign-keys show cdetailview

我有一个表名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之类的数据,并在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中获得城市名称,但我怎样才能获得sector,street_number?

3 个答案:

答案 0 :(得分:0)

从您的数据库架构中我可以看到一个企业可以拥有多个地址。 如果要显示所有这些内容,可以使用以下内容:

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

答案 1 :(得分:0)

好的,这是交易,我有两个表,businessaddress,地址表有business _ id键作为外键,我想显示一个{{ 1}} 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; } ?> 视图中,我可以回复street_number等我想要的地方。就像下面一样。

userbusiness.php

答案 2 :(得分: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,
    ));
}

最后,在你的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'
        ),
    ),
)); ?>