使用与 CGridView 的关系

发布于 2024-09-09 09:46:40 字数 1389 浏览 1 评论 0原文

我将尽可能清楚地描述该问题。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷→糖丶微凉 2024-09-16 09:46:40

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',),

旧情勿念 2024-09-16 09:46:40

当您的关系是 HAS_MANY 时,调用该关系将返回该 ActiveRecord 的数组。 CGridView 不会显示数组,因此我过去所做的是在模型中编写一个函数,其中包含获取数组中所有值并从中创建字符串的关系。例如,在菜单模型中的示例中执行类似的操作,

public function rolesToString()
{
    $roles = $this->roles;
    if($roles) {
        $string = '';
        foreach($roles as $role) {
            $string .= $role->can_view . ', ';
        }
        return substr($string,0,strlen($string)-1); // substr to remove trailing comma
    }
    return null;
}

那么该列的 CGridView 的“value”属性应如下所示

'value'=>'$data->rolesToString()'

有一些有关使用 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

public function rolesToString()
{
    $roles = $this->roles;
    if($roles) {
        $string = '';
        foreach($roles as $role) {
            $string .= $role->can_view . ', ';
        }
        return substr($string,0,strlen($string)-1); // substr to remove trailing comma
    }
    return null;
}

then the 'value' attribute of the CGridView for that column should look like this

'value'=>'$data->rolesToString()'

There is some more information about using HAS_MANY, with, and CGridView Here

不打扰别人 2024-09-16 09:46:40

清除最后一个逗号的最佳方法是..
修剪(',',字符串)
问候

the best way to clean out the last comma is..
trim(',',string)
regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文