Responsive Ad Area

Share This Post

test

YII2 insert data from CheckBoxList

In my Form :

 <?= GridView::widget([
 'id' => 'griditems',
 'dataProvider' => $dataProvider,
  'columns' => [
   'receiver_name',
   'receiver_phone',
   'item_price',
   'shipment_price',
   ['class' => 'yiigridCheckboxColumn'],
  ],

]); ?>

In my Controller :

public function actionCreate()
{
    $model = new Package();
    $searchModel = new ShipmentSearch();
    $searchModel->shipment_position=1; // الشحنه في مقر الشركة
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if ($model->loadAll(Yii::$app->request->post())){ 
        $select = Yii::$app->request->post('selection');

        foreach($select as $id){
            $shipment= shipment::findOne((int)$id);
            $model->company_id=1;
            $model->shipment_id=$shipment->id;
            $model->send_from=$shipment->send_from;
            $model->send_to=$shipment->send_to;
            $model->save(false);

        }
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'dataProvider'=>$dataProvider,
        ]);
    }
}

I just try this way to insert data as above its works but the data inserted just last one or last CheckBox from the list . I tried to print the id that in foreach and there is more than one id


YII2 insert data from CheckBoxList
YII2 insert data from CheckBoxList
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar