E
E
evgeniy_matveev2018-07-31 12:22:34
Yii
evgeniy_matveev, 2018-07-31 12:22:34

Why is data not saved to the database when using Select2?

Hello. I'm trying to use the select2 widget from kartik-v in my project.
There is a model:

<?php
namespace app\models;
use yii\db\ActiveRecord;
use Yii;

class Delivery extends ActiveRecord{
    public static function tableName()
    {
        return 'delivery';
    }
    public function attributeLabels()
    {
        return [

            'id' => 'ID',
            'parent_id' => 'Parent ID',
            'materials_electric' => 'Список рассылки для материалов электрической части',
            'materials_mehanic' => 'Список рассылки для материалов механической части',
            'design_electric' => 'Список рассылки для проектирования электрической части',
            'design_mehanic' => 'Список рассылки для проектирование механической части',
            'installing_electric' => 'Список рассылки для монтажа электрической части',
            'installing_mehanic' => 'Список рассылки для монтажа механической части',
            'building' => 'Список рассылки для производства',


        ];
    }

    public function rules()
    {
        return [
            ['parent_id', 'required'],
            [['materials_electric','materials_mehanic','design_electric','design_mehanic','installing_electric','installing_mehanic','building'],'trim']

        ];
    }

   
    public function getProjects(){
        return $this->hasOne(Projects::className(),['id'=>'parent_id']);
    }

}

To write to the database, I used GII Ajax CRUD.
I want the input fields to be written with a string that the user types using the select2 widget.
The widget is set up like this:
<?= $form->field($model, 'materials_electric')->widget(Select2::classname(), [
        'data' => $data,
        'options' => [
            'placeholder' => ' ',
            'multiple' => true
        ],
    ]) ?>

The problem is that when using textInput() the record is stored in the database, but when using select2 it is not. The data format in mysql is varchar(200) utf8_general_ci. I'm new to the web and don't know much about debugging. What's my mistake?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2018-07-31
@evgeniy_matveev

I think you have an array there, therefore, before saving (and possibly before validation), it must be cast to a string. Try adding to the model:

public function beforeValidate() {
        if(is_array($this->materials_electric)){
                   $this->materials_electric = implode(",", $this->materials_electric);
        }
        return parent::beforeValidate();
    }

and remove materials_electric from the trim validator, as I understand it, it needs a string. Although if we are talking about multiple choices, then it’s probably worth either converting to json or even having a separate table associated to store the selected options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question