Answer the question
In order to leave comments, you need to log in
How to work with json fields in yii2?
I recently found out that Yii2 has support for Json fields, so I decided to rewrite the old code, but ran into difficulties.
When receiving information, there are no problems, you do not need to decode the field yourself, the value comes already in the form of an array. But there is a problem when trying to change the values.
The value is not displayed correctly in the form. "Array" is displayed
<?= $form->field($model, 'meta')->textInput(['maxlength' => true]) ?>
$model->meta = json_encode($model->meta);
Array
(
[_csrf] => VmUwDTNtfnkj2HqHmZJyxfaKMTC_IFElEnXlXuJloTcOKmVXUQQxNk2HLb7TxCChhflVZdUUBVJjRI4auDPqDg==
[Sale] => Array
(
...
[meta] => {<b>"sub_CHANGED":3</b>,"icon":"fal fa-gift","text":"-70%","price":1650,"oldPrice":5500}
...
)
)
$model->load(Yii::$app->request->post()
there is no change in the data.app\models\Sale Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
...
[meta] => {<b>"sub":3</b>,"icon":"fal fa-gift","text":"-70%","price":1650,"oldPrice":5500}
...
)
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