Ujj*_*ati 2 orm activerecord yii
我的控制器
$criteria = new CDbCriteria();
$criteria -> select = 't.*,b.*';
$criteria -> join = 'INNER JOIN tbl_b b on b.b_id = t.id ';
$criteria -> join .= 'INNER JOIN tbl_c c on c.id = b.c_id';
$criteria -> condition = 'c.id = :cid';
$criteria -> params = array(':cid' => 1);
$dataProvider = new CActiveDataProvider('tbl_a',array(
'criteria' => $criteria
));
$this->render('view',array('dataProvider' => $dataProvider));
Run Code Online (Sandbox Code Playgroud)
我的看法
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'my-grid',
'dataProvider' => $dataProvider,
'columns' => array(
'name',
'description',
array(
'header' => 'Column from tbl_b',
'value' => ''
),
array(
'class'=>'CButtonColumn',
'template' => '{view}'
), ),));
Run Code Online (Sandbox Code Playgroud)
我的问题是:我怎样才能显示列的值从tbl_b.因为in dataprovider,我已经指定tbl_a,它只是从中提取数据tbl_a而不是tbl_b我已经从中选择了所有记录tbl_b.
根据我所学到的,它应该显示为$data -> tbl_b -> col_b.但这是错误,因为tbl_a.tbl_b没有定义.我想知道这是什么问题?
Is it something regarding the relation? tbl_a和tbl_c相关MANY_MANY经过tbl_b.即tbl_b具有两个柱连接的主ID tbl_a和tbl_c.
注意:名称和描述来自tbl_a并显示它们.
请指教!
我通常为这种事做的是在ActiveRecord中创建一个属性.
class A extends CActiveRecord {
public $bAttribute1
}
Run Code Online (Sandbox Code Playgroud)
当我A与表连接表时B,我重命名B我想要显示的字段与我创建的属性(在这种情况下,bAttribute)
$dataProvider = new CActiveDataProvider('A', array(
'criteria' => array(
'select' => array('t.*', 'b.attribute1 AS bAttribute1'),
'join' => 'JOIN B AS b ON b.joinId = t.id',
)
));
Run Code Online (Sandbox Code Playgroud)
然后我可以bAttribute1在GridView中显示,
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'bAttribute1',
)));
Run Code Online (Sandbox Code Playgroud)
这应该有效.但缺点是,如果要显示连接表中的大量列,则必须创建许多属性.
UPDATE
很奇怪,所以我试着从头开始做例子.我创建了两个表ModelA,ModelB如下所示.
CREATE TABLE IF NOT EXISTS `ModelA` (
`id` int(11) NOT NULL,
`attribute2` int(11) NOT NULL,
`attribute3` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `ModelB` (
`id` int(11) NOT NULL,
`aId` int(11) NOT NULL,
`attribute3` int(11) NOT NULL,
`attribute4` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)
如您所见,aIdin ModelB是一个外键ModelA.然后我举了一些例子.
INSERT INTO `ModelA` (`id`, `attribute2`, `attribute3`) VALUES
(1, 1, 1),
(2, 2, 2);
INSERT INTO `ModelB` (`id`, `aId`, `attribute3`, `attribute4`) VALUES
(1, 1, 10, 100),
(2, 2, 20, 200),
(3, 1, 30, 300),
(4, 1, 40, 400);
Run Code Online (Sandbox Code Playgroud)
因此,条目#1 ModelA将被3个条目引用,ModelB而条目#2将仅有1个条目.
然后我创建了模型代码.
class ModelA extends CActiveRecord {
public $bAttribute3;
public $bAttribute4;
public static function model($className = __CLASS__) {
return parent::model($className);
}
}
Run Code Online (Sandbox Code Playgroud)
看到bAttribute3和bAttribute4在模型中,我分别把表attribute3和attribute4那里的ModelB表放在那里.然后在控制器中我创建了DataProvider
public function actionIndex(){
$dataProvider = new CActiveDataProvider('ModelA', array(
'criteria' => array(
'select' => array(
'`t`.*',
'`b`.`attribute3` AS `bAttribute3`',
'`b`.`attribute4` AS `bAttribute4`'
),
'join' => 'JOIN `ModelB` AS `b` ON `b`.`aId` = `t`.`id`',
)
));
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}
Run Code Online (Sandbox Code Playgroud)
在视图中,我做到了这一点
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'my-grid',
'dataProvider' => $dataProvider,
'columns' => array(
'id',
'attribute2',
'attribute3',
'bAttribute3',
'bAttribute4',
),
));
Run Code Online (Sandbox Code Playgroud)
所以,这就是我所看到的

这是你想要的吗?即使对于CActiveDataProvider,我通常也会这样做.我想你的代码可能会遗漏一些东西.
| 归档时间: |
|
| 查看次数: |
6756 次 |
| 最近记录: |