使用与 CGridView 的关系
我将尽可能清楚地描述该问题。
我有 activeDataProvider:
$dataProvider=new CActiveDataProvider('Menu', array(
'criteria'=>array(
'with' => array('roles'),
),
));
然后我使用带有复选框的 CGridView:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'menu-grid',
'selectableRows' => 2,
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'title',
array(
'class' => 'SCheckboxColumn',
'header' => 'View',
'name' => 'Roles[Actions][can_view]',
'id' => 'roles_action_can_view',
'value' => '$data->id',
'checkBoxHtmlOptions' =>
array('checked' => $data->roles->can_view),
),
),
));
然后在菜单模型关系中:
return array(
'roles' => array(self::HAS_MANY, 'Rolesmenus', 'menu_id'),
);
和在 Rolesmenus 模型关系中:
return array(
'menu' => array(self::BELONGS_TO, 'Menu', 'menu_id'),
);
所以,当我 var_dump 所有 $data 对象时,我无法访问 $data->roles->can_view 变量可以在 _attributtes 私有数组中看到这些属性,但我无法通过 CGridView 访问它们。
有什么想法吗?
I'll describe the problom as clearly as I can.
I have activeDataProvider:
$dataProvider=new CActiveDataProvider('Menu', array(
'criteria'=>array(
'with' => array('roles'),
),
));
then I'am using CGridView with checkbox:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'menu-grid',
'selectableRows' => 2,
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'title',
array(
'class' => 'SCheckboxColumn',
'header' => 'View',
'name' => 'Roles[Actions][can_view]',
'id' => 'roles_action_can_view',
'value' => '$data->id',
'checkBoxHtmlOptions' =>
array('checked' => $data->roles->can_view),
),
),
));
Then in Menu Model relations:
return array(
'roles' => array(self::HAS_MANY, 'Rolesmenus', 'menu_id'),
);
and in Rolesmenus Model relations:
return array(
'menu' => array(self::BELONGS_TO, 'Menu', 'menu_id'),
);
So, I cant access $data->roles->can_view variable, when I var_dump all $data object I can see these attributes in _attributtes private array but I cant them access through CGridView.
Any ideas??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
array('checked' => $data->roles->can_view,),
需要
array('checked' => '$data->roles->can_view',),
array('checked' => $data->roles->can_view,),
needs to be
array('checked' => '$data->roles->can_view',),
当您的关系是 HAS_MANY 时,调用该关系将返回该 ActiveRecord 的数组。 CGridView 不会显示数组,因此我过去所做的是在模型中编写一个函数,其中包含获取数组中所有值并从中创建字符串的关系。例如,在菜单模型中的示例中执行类似的操作,
那么该列的 CGridView 的“value”属性应如下所示
有一些有关使用 HAS_MANY、with 和 CGridView 此处
When your relation is a HAS_MANY then calling that relation will return an array of that ActiveRecord. The CGridView won't display an array, so what I have done in the past is write a function in the model that contains the relation to take all the values in the array and create a string from them. For example with your example in the menu model do something like this
then the 'value' attribute of the CGridView for that column should look like this
There is some more information about using HAS_MANY, with, and CGridView Here
清除最后一个逗号的最佳方法是..
修剪(',',字符串)
问候
the best way to clean out the last comma is..
trim(',',string)
regards