我有 2 张桌子。表有关系 (HAS_MANY)
table1: user(id,name) table2:address(id,userId,address)
用户可以有一些地址我用痣定义关系:user.php 和 address.php
用户.php
'address' => array(self::HAS_MANY, 'address', 'userId'),
地址.php
'user' => array(self::BELONGS_TO, 'user', 'userId'),
当我写
$dataProvider = new CActiveDataProvider('user')
我只得到用户表的记录,但我想获取两个表的记录,我想为每个用户获取名称、数组(地址),该怎么做?
用户控制器.php
public function actionIndex() {
$dataProvider = new CActiveDataProvider('User');
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}
索引.php
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
_view.php
<b><?php echo $data->name)); ?>: <?php $data->address->address; ?>:</b>