1

My problem is when I select data from the check box and click save button, I do not know where it goes. Do I need to make another table or add column in events table.enter image description here This is the coding for index.php for events.

    <?php

    use yii\helpers\Html;
    use yii\grid\GridView;

    /* @var $this yii\web\View */
    /* @var $dataProvider yii\data\ActiveDataProvider */

    $this->title = 'Events';

    $this->params['breadcrumbs'][] = $this->title;
    ?>

    <div class="events-index">

        <h1><?= Html::encode($this->title) ?></h1>
        <p>
            <?= Html::a('Create Events', ['create'], ['class' => 'btn btn- success']) ?>
        </p>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],


                'event_title',
                'event_date',
                'event_location:ntext',

                ['class' => 'yii\grid\ActionColumn'],
                ['class' => 'yii\grid\CheckBoxColumn'],
            ],
        ]); ?>
        <?=Html::beginForm(['events/bulk'],'post');?>

    <center><?=Html::submitButton('Save', ['class' => 'btn btn-info',]);?> </center>
    <?= Html::endForm();?> 



    </div>

This is the code for action bulk in eventscontroller.php . I'm new in yii2, so I dont know how to edit this following code so that when I click the save button after select event it will save to the database.

        public function actionBulk()
       {

       $selection=(array)Yii::$app->request->post('selection');//typecasting
       foreach($selection as $id){
            $e=Events::findOne((int)$id);//make a typecasting
            //do your stuff
            $e->save();
       }
         }

Hope that someone can help me to fix the problem. I'm really appreciate it. Thank you.

Fyp16
  • 131
  • 1
  • 5
  • 16

1 Answers1

0

Modify gridview config for this checkbox column so it can return model ID instead of table key:

[
    'class' => 'yii\grid\CheckBoxColumn',
    'checkboxOptions' => function ($model, $key, $index, $column) {
        return ['value' => $model->id];
    },
],
Bizley
  • 17,392
  • 5
  • 49
  • 59
  • Actually after click the save button, I wonder in which table in database it will be saved. Or I need to create another model for the selected check box data? I'm really sorry because it is very difficult to me @Bizley – Fyp16 Nov 23 '16 at 11:51
  • I can only answer you based on the code you provided and I can see you try to save it in the table set in the `Events` class. – Bizley Nov 23 '16 at 11:55