Yii2 how to bulk update

I’m trying to update multiple itens using selected rows in checkBoxColumn (Kartik Grid).I’ve looked at many examples and I can’t seem to get it to work.

In my views/usuarios/index.php I have this JS script:

$this->registerJs(' 

    $(\'#desativar\').on(\'click\',function(e){
        e.preventDefault;

        var keys = $(\'#usuariosLdap-grid\').yiiGridView(\'getSelectedRows\');

        $.ajax({
            type: \'POST\',
            cache: false,
            data: {keylist: keys},
            url: \''.Url::to(['/usuarios/updatemultiple']).'\', 
            success: function (response) {conlose.log(\'success\');},
            error: function(){console.log(\'failure\');}
        });

        return false;

    });


');

Still in the views/usuarios/index.php I have the Grid:

echo DynaGrid::widget([
        'columns'=> $gridColumns,
        'storage'=>'session',
        'theme'=>'panel-secondary',
        'showPersonalize'=>true,
        'allowFilterSetting' => False,
        'allowSortSetting' => False,
        'allowThemeSetting' => False,
        'gridOptions'=>[
            'dataProvider'=> $dataProvider,
            'filterModel'=> $searchModel,
            'options' => ['id' => 'usuariosLdap-grid'],
            // 'floatHeader' => true,
            'pjax' => true,
            'panel'=>['heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-user"></i> Usuários LDAP</h3>'],
            'toolbar' => [
                [
                    'content'=>
                       Html::a('<i class="glyphicon glyphicon-plus"></i> Inserir', ['create'], ['class' => 'btn btn-success']) . '  ' .
                       Html::a('<i class="glyphicon glyphicon-remove"></i"></i> Desativar', ['index'], ['class' => 'btn btn-warning', 'title' => 'Desativar', 'id'=>'desativar',]) . '  ' .
                       Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => Yii::t('kvgrid', 'Reset Grid')]),
                ],
                ['content'=>'{dynagrid}'],
                '{export}',
                '{toggleData}'
            ],

Now the controllers/UsuariosController.php:

public function actionUpdatemultiple()
    {
        
        if(Yii::$app->request->post('keylist'))
        {
            $keys = Yii::$app->request->post('keylist');


            foreach ($keys as $key)
            {
               $sql = "UPDATE querover  SET status=0 HWERE id = $key";
                $query = Yii::$app->db->createCommand($sql)->execute();
            }
        }

         return $this->redirect(['index']);

    }

It doesn’t matter what I do, I always get 500 (Internal Server Error):

enter image description here

Could anyone please help me?

Welcome to the forum!

You can use the Yii debugger to find out the reason for the 500 error.
Select the ajax request then select the log.

You have a couple of typos:

and

1 Like

Hi, Tommy!
I spent 2 days searching, thinking it was a problem with the JavaScript code and I hadn’t seen the SQL typo (HWERE). That was it! Thank you so much for your help!
And thank you for the Yii debugger tip, I wasn’t properly aware of it. :slight_smile: