D
D
Dmitry2019-03-28 12:18:05
Yii
Dmitry, 2019-03-28 12:18:05

How to display the value of a SET type field in a form using a checkboxlist?

The MySQL table has a field days of type SET, which is a set of days of the week. The model in Yii receives from the base and returns the days attribute as a string, where the values ​​are separated by a comma. Using:

$form->field($model, 'days')->checkboxList(['Пн'=>'Пн', 'Вт'=>'Вт'...]);

checkboxes remain unchecked, apparently the value is expected to be an array. How would it be so clever to replace the string with an array so that this code works? Well, or with the least effort, rewrite this code so that the days of the week are marked in the form in accordance with the database data.
UPD Apparently, you need to create an additional attribute daysArray and store the value in it as an array. But two events are needed: after the model receives data and before writing. Found the second event: BeforeSave, I can not find the event immediately after reading the data from the database.

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