H
H
hollanditkzn2017-05-05 17:25:35
Yii
hollanditkzn, 2017-05-05 17:25:35

How to add multiple records to database?

I have implemented yii2-multiple-input widget And I don't understand some things about this widget. I need to add multiple records to a db.
Implemented in the model

class Custom extends \yii\db\ActiveRecord
{
    public $customs;
public function attributeLabels()
    {
        return [
      ...
            'customs'
        ];
    }

In the controller
$custom->load(Yii::$app->request->post()) && Yii::$app->db->createCommand()->batchInsert('Custom',['tovar','number'],[])->execute()) {
           return $this->redirect(['shop']); 
        }

And in view
<?php $form = ActiveForm::begin([
    	'enableAjaxValidation'		=> true,
    	'enableClientValidation' 	=> false,
    	'validateOnChange' 			=> false,
    	'validateOnSubmit'			=> true,
    	'validateOnBlur' 			=> false,
    ]); ?>
    <div id="customForm">

    <?= $form->field($custom, 'customs')->widget(MultipleInput::className(), [
    	'max' => 6,
    	'columns' => [
    		[
    			'name' => 'tovar',
    			'type' => 'textInput',
    			'title' => 'Товар',
    		],
    		[
    			'name' => 'number',
    			'type' => 'textInput',
    			'title' => 'Кол-во',
    			'options' => [
    			'type' => 'number',
    			'min' => '0'
    			]
    		]
    	],
    ]) ?>
    </div>
    <div class="form-group">
        <?= Html::submitButton($custom->isNewRecord ? 'Создать' : 'Редактировать', ['class' => $custom->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

Of course, how do I get data from customs to take data?
In debug, it displays the following data Custom[customs]:
Custom[customs][0][product]:aptap
Custom[customs][0][number]:10

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question