我正在尝试连接CSqlDataProvider上的一个关系表profiles,但它不是工作属性。
如何在视图中访问profiles.id?
Controller.php:
$sql = "SELECT `events`.`id`
FROM events
LEFT OUTER JOIN `profiles` ON (`events`.`profile_id`=`profiles`.`id`)";
$dataProvider=new CSqlDataProvider($sql);View.php:
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'striped bordered condensed',
'id'=>'events-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'profiles.id', // Problem here, always returns NULL.
'events.id',
),
)); ?>发布于 2013-01-23 21:50:19
select另一个字段,就像在Phillipe's answer中已经提到的:选择events.id,profiles.id ...
profiles.id一个别名,因为两个列名都是id:选择events。id,profiles。id作为pid ...
‘profiles.id’=>array( 'pid',//而不是列'id',//不是events.id )
发布于 2013-01-23 03:29:26
您可以将其添加到SQL查询中
$sql = "SELECT `events`.`id`, `profiles`.`id`
FROM events
LEFT OUTER JOIN `profiles` ON (`events`.`profile_id`=`profiles`.`id`)";但是您的事件中似乎已经有了profile.id,所以您可以在视图中使用以下代码:
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'striped bordered condensed',
'id'=>'events-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'profile_id',
'events.id',
),
)); ?>https://stackoverflow.com/questions/14466413
复制相似问题