2016-11-23 57 views
1

我的问题是,当我从复选框中选择数据并单击保存按钮时,我不知道它在哪里。我是否需要制作其他表格或在事件表中添加列。 enter image description here 这是事件的index.php的编码。如何将选中的数据从复选框保存到数据库

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

这是eventscontroller.php中批量操作的代码。我是yii2中的新成员,所以我不知道如何编辑以下代码,以便在选择事件后单击保存按钮时,它将保存到数据库。

 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(); 
     } 
     } 

希望有人能帮助我解决问题。我真的很感激它。谢谢。

回答

0

修改此复选框列GridView的配置,以便它可以返回,而不是表重点型号ID:

[ 
    'class' => 'yii\grid\CheckBoxColumn', 
    'checkboxOptions' => function ($model, $key, $index, $column) { 
     return ['value' => $model->id]; 
    }, 
], 
+0

实际点击保存按钮后,我不知道在表中的数据库将被保存。或者我需要为选中的复选框数据创建另一个模型?我很抱歉,因为这对我很困难@Bizley – Fyp16

+0

我只能根据你提供的代码回答你,我可以看到你尝试将它保存在'Events'类的表中。 – Bizley

相关问题