Answer the question
In order to leave comments, you need to log in
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'
];
}
$custom->load(Yii::$app->request->post()) && Yii::$app->db->createCommand()->batchInsert('Custom',['tovar','number'],[])->execute()) {
return $this->redirect(['shop']);
}
<?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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question