In yii2 I have an associative array, I try to save it in JSON
type MySQL table's field. However, the saved value is corrupted JSON object with \
back slashes.
The above screenshot from phpmyadmin. I saving the data by the code below:
// In the update action:
//...
if ($flag) {
$transaction->commit();
Yii::$app->getSession()->setFlash('success', 'Record has been updated!');
$this->savedi($model);
return $this->redirect(['view', 'id' => $model->id]);
}
//...
private function savedi($model)
{
$output = [];
foreach ($model->invoiceItems as $i => $item){
$output['items'][$i]['id'] = $item->item_id;
$output['items'][$i]['title'] = $item->item->title;
$output['items'][$i]['qty'] = $item->qty;
}
$model->saved = json_encode($output);
$model->save();
}
I don't know why the JSON is corrupted like that?