简体   繁体   English

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

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

I'm trying to add the following functionality, however I'm not sure where to start. 我正在尝试添加以下功能,但是我不确定从哪里开始。 Any advice, examples, or direction would be greatly appreciated. 任何建议,示例或指导将不胜感激。

I want to add button to the cgridview of the main model in this context. 在这种情况下,我想向主模型的cgridview添加按钮。 Each of the records available in the cgridview for this model, have a unique attribute called lot for example R3XSEF9 该模型的cgridview中可用的每个记录都有一个名为lot的唯一属性,例如R3XSEF9

There is another secondary table/model in my database that has records with this same lot attribute. 我的数据库中还有另一个辅助表/模型,其中的记录具有相同的lot属性。 However, this table only has certain records out of all the possible records, sometimes duplicates, and has a set of different attributes. 但是,此表在所有可能的记录中仅包含某些记录,有时是重复的,并且具有一组不同的属性。

What I'd like to do is, using the lot attribute, for example lot R3XSEF9 from my cgridview, search the secondary table to see if there is one ore more corresponding rows which contains that same lot R3XSEF9. 我想做的是,使用lot属性,例如从cgridview获取的lot R3XSEF9,搜索辅助表以查看是否有一个或多个对应的行包含相同的lot R3XSEF9。

If so, I would like the button to be displayed in my CButtonColumn and link to the views for those corresponding models of the secondary table. 如果是这样,我希望按钮显示在我的CButtonColumn中,并链接到辅助表的那些相应模型的视图。 If not, I would like no button to appear. 如果没有,我希望没有按钮出现。

Thanks for any help. 谢谢你的帮助。 If any clarification is required, I would be happy to do so. 如果需要任何澄清,我很乐意这样做。

First of all you need to link tables using "relations" function in model class. 首先,您需要使用模型类中的“关系”函数链接表。 If you use FOREIGN KEY constraint in DB relations already filled. 如果使用FOREIGN KEY约束,则已经填充的数据库关系。

SQL statement: SQL语句:

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

Model class: 型号类别:

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

Then you can use custom button column in your grid (view file) like this: 然后,您可以使用网格(视图文件)中的自定义按钮列,如下所示:

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

Explanation of button parameters to be passed thru "buttons" array you can find here . 您可以在此处找到通过“ buttons”数组传递的按钮参数的说明。 Especialy this part: 特别是这部分:

buttons property 按钮属性

public array $buttons; 公共数组$ buttons;

the configuration for additional buttons. 其他按钮的配置。 Each array element specifies a single button which has the following format: 每个数组元素都指定一个具有以下格式的按钮:

'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