简体   繁体   中英

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. Each of the records available in the cgridview for this model, have a unique attribute called lot for example R3XSEF9

There is another secondary table/model in my database that has records with this same lot attribute. 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.

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. 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.

SQL statement:

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 . Especialy this part:

buttons property

public array $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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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