'PHP: Save PHP Array to MySQL json field value changed

I found a weird thing and I'd like to know if anyone know the reason.

I'm using php framework symfony and doctrine to persist data. There is a field score_detail marked as json_array which in return become a json data type in database. Following is the data structure

[{
    "game_end": null,
    "game_key": "b5ee85e6-6d1c398f-d5b0d721-b0dbe376-6529c32c",
    "game_index": 1,
    "game_score":
    [
        {
            "team_id": 15,
            "score_stat":
            [
                1,
                1,
                1,
                1,
                1
            ]
        },
        {
            "team_id": 14,
            "score_stat":
            [
                0,
                0,
                0,
                0,
                0
            ]
        }
    ],
    "game_start": "2022-05-21 10:17:21",
    "game_winner_team_id": null
}]

When I finish update the score_detail which is an array in php, then I persist it and save like the following

$scoreObject->setScoreDetail($score_detail);
$scoreRepository->save($scoreObject)

After the update, I inspect the data from the table. It becomes like

[{
"game_end": null,
"game_key": "b5ee85e6-6d1c398f-d5b0d721 [...]",
"game_index": 1,
"game_score":
[
    {
        "team_id": 15,
        "score_stat":
        [
            1,
            1,
            1,
            1,
            1,
            1
        ]
    },
    {
        "team_id": 14,
        "score_stat":
        [
            0,
            0,
            0,
            0,
            0,
            0
        ]
    }
],
"game_start": "2022-05-21 10:17:21",
"game_winner_team_id": null

}]

The game_key turns into a weird thing

b5ee85e6-6d1c398f-d5b0d721 [...]

This is what confuses me. But if I twist the above code a bit, it then works as expected.

$scoreObject->setScoreDetail(json_decode(json_encode($score_detail), true));
$scoreRepository->save($scoreObject)

I json_encode and then json_decode it again to get the correct result. Does anyone know the reason? Thanks.



Solution 1:[1]

How is the data currently stored? You could .gitignore the files you don't wish to push if saved within your directory, and follow these directions to store your code in the repo from the GitHub command line.

https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line

You could alternatively create the repo in GitHub and link it to your local repo: https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/importing-a-git-repository-using-the-command-line

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Melanie Ryan