简体   繁体   中英

Yii CButtonColumn updating Database directly

So I have an icon in my CButtonColumn, is it possible to make it in a way that by clicking on that icon, it direct updates a specific field of the record in the database?

For example,

A booking record which has a field Complete . By clicking on the icon, it automatically changes the value of Complete from 0 to 1.

Is it possible to do something like that? If so, how? I tried adding the entire SQL query into the urlExpression like,

Yii::app()->db->createCommand('UPDATE booking SET complete = 1 WHERE id = 1')

But it's not working.

Please help me, Thanks in advance.

UPDATE based on hamed's comment

'changeComplete' => array(
                'imageUrl' => 'images/complete.png',
                'options' => array(
                    'title' => 'Complete',
                    //'style' => 'cursor:pointer; margin: 0 2px;'
                ),
                'url' => 'Yii::app()->createUrl("booking/changeComplete", array("id"=>$data->id))',
                'click' => "function(){
                    $.fn.yiiGridView.update('id', {
                        type:'POST',
                        url:$(this).attr('href'),
                        success:function(data) {
                            $.fn.yiiGridView.update('id');
                        }
                    })
                    return false;
                }",
            ),

in the booking Controller:

public function actionChangeComplete($id)
{
    Yii::app()->db->createCommand('UPDATE booking SET complete = 1 WHERE id = $id');
}

but it is still not working when clicking on the button. Did I do something wrong? Please advise thanks

First thing you should know is database transactions should be executed inside controller, not view . So you should try an action for doing that. Try this inside your gridView

array(
    'class' => 'CButtonColumn',
    'template' => '{changeComplete}',
    'buttons' => array(
        'changeComplete' => array(
            'label' => Your button label',
            'imageUrl' => false,
            'options' => array(
                'title' => 'title of your button',
                'style' => 'cursor:pointer; margin: 0 2px;'
            ),
            'url' => 'Yii::app()->createUrl("test/changeField", array("id"=>$data->id))',
            'click' => "function(){
                        $.fn.yiiGridView.update('gridID', {
                        type:'POST',
                        url:$(this).attr('href'),
                        success:function(data) {
                        $.fn.yiiGridView.update('gridID');
                        }
                    })
                           return false;
                 }
                ",
        ),

    )
),

Now, you need to define actionChangeField inside TestController (controller and action names just are example, you should define an action with suitable name in suitable controller)

public function actionChangeField($id)
{
      Yii::app()->db->createCommand('UPDATE booking SET complete = 1 WHERE id = $id')->execute();
}

Finally, replace "gridID" with id of your grid(without # sign).

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