繁体   English   中英

Yii为另一个模型中存在的字段创建CButton列

[英]Yii create CButton Column for field that exists in another model

我正在尝试添加以下功能,但是我不确定从哪里开始。 任何建议,示例或指导将不胜感激。

在这种情况下,我想向主模型的cgridview添加按钮。 该模型的cgridview中可用的每个记录都有一个名为lot的唯一属性,例如R3XSEF9

我的数据库中还有另一个辅助表/模型,其中的记录具有相同的lot属性。 但是,此表在所有可能的记录中仅包含某些记录,有时是重复的,并且具有一组不同的属性。

我想做的是,使用lot属性,例如从cgridview获取的lot R3XSEF9,搜索辅助表以查看是否有一个或多个对应的行包含相同的lot R3XSEF9。

如果是这样,我希望按钮显示在我的CButtonColumn中,并链接到辅助表的那些相应模型的视图。 如果没有,我希望没有按钮出现。

谢谢你的帮助。 如果需要任何澄清,我很乐意这样做。

首先,您需要使用模型类中的“关系”函数链接表。 如果使用FOREIGN KEY约束,则已经填充的数据库关系。

SQL语句:

CREATE TABLE Model1
(
    ...
    FOREIGN KEY(lot) REFERENCES MainModel(lot) ON UPDATE CASCADE ON DELETE RESTRICT,
    ...
)

型号类别:

 class MainModel extends CActiveRecord
 {
         ...

    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'lots' => array(self::HAS_MANY, 'Model2', 'lot'),
        );
    }

然后,您可以使用网格(视图文件)中的自定义按钮列,如下所示:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'main-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
    ...
    array(
        'class' => 'CButtonColumn',
        'template' => '{lots}',
        'header' => 'Lots',
        'buttons' => array(
            'lots' => array(
                'label' => 'Lots',
                'imageUrl' => Yii::app()->request->baseUrl.'/img/....png',
                'url' => 'Yii::app()->createUrl("controller1/lotlistbymainid", array("id" => $data->id))',
                'visible' => 'count($data->lots) > 0',
            ),
        ),
    ),

您可以在此处找到通过“ buttons”数组传递的按钮参数的说明。 特别是这部分:

按钮属性

公共数组$ buttons;

其他按钮的配置。 每个数组元素都指定一个具有以下格式的按钮:

'buttonID' => array(
    'label'=>'...',     // text label of the button
    'url'=>'...',       // a PHP expression for generating the URL of the button
    'imageUrl'=>'...',  // image URL of the button. If not set or false, a text link is used
    'options'=>array(...), // HTML options for the button tag
    'click'=>'...',     // a JS function to be invoked when the button is clicked
    'visible'=>'...',   // a PHP expression for determining whether the button is visible
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM