1

In 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.

enter image description here

The above screenshot from . 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?

5
  • Have a look into: stackoverflow.com/a/22648879/7889695 Commented Jun 20, 2022 at 11:35
  • I have back slashes. and I tested it and the object's string in phpmysdmin still the same. Commented Jun 20, 2022 at 11:43
  • Now I see, that is complete normal, because special characters are escaped inside a MYSQL string. If you load back your model and var_dump/echo/xdebug it, you will see the backslashes are gone. Ref: github.com/yiisoft/yii2/blob/… github.com/yiisoft/yii2/blob/… Commented Jun 20, 2022 at 12:22
  • 1
    It looks like double encode to me. Yii2 has some support for JSON fields and it's doing json_encode by itself. Try $model->saved = $output instead of encoding it yourself. Commented Jun 20, 2022 at 12:24
  • @MichalHynčica Oh Yes! that works fine. Thank you. Please add this hint as answer. Commented Jun 20, 2022 at 12:27

1 Answer 1

1

The string in question looks like double encoded JSON.

The yii2 has some basic support for JSON fields which take care of encoding/decoding. So instead of calling json_encode() yourself you just need to assign array/object that should be encoded as it is.

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 = $output;
    $model->save();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.