dimanche 25 novembre 2018

Iterate nested objects in Yii2 php

I need iterate nested objects to form a proper JSON file with the following structure:

"stories": [
    {
        "storyID": "66d2efb9-2e16-4251-8e9d-8caa44323f81",
        "status": 0,
        "categoryID": "0345aa43-7fd8-4f32-a65a-143da19df423",
        "details": {
            "title": "Lily Green"
        },
        "seasons": [
            {
                "seasonID": "b6ccd3c7-ddd4-4044-a342-2f167532f2f2",
                "status": 0,
                "details": {
                    "title": "Season0",
                    "body": "He called me his little girl. The father could do anything he wanted with his little girl. 10 years ago he forced me to kill my mother. Today I’m gonna kill him.",
                    "previewAssetID": "e355a090-c10c-4feb-82c6-cf0279408b6b",
                    "coverAssetID": "e8cc3da6-03b4-46f4-8dce-bc02cf143f8d"
                },
                "episodes": [
                    {
                        "episodeID": "8c202576-d79f-4acc-8ea9-a8fa9600da9a",
                        "status": 0,
                        "preload": false,
                        "details": {
                            "title": "EpisodeTitle0"
                        },
                        "assetID": "67187c26-386b-419e-a30f-43fd4ff7ab8a"
                    }
                ]
            }
        ]
    }
]

Appropriately I have entities called Story, Season and Episode. Story has many Season and Season has a lot of Episode I need to find some way how I can iterate those type of objects without using directly Yii2 relations like :

foreach($stories as $story){
   -----
   foreach($story->seasons as $season){
     ---
        foreach($season->episodes as $episode){

        }
   }
}

Above example didn`t suit for me cause it is possible that this structure will have bigger depth than now. I have already read about Iterator pattern, but to be honest, I can't realize how I can use it in my case. I will be appreciated for any advice.

Aucun commentaire:

Enregistrer un commentaire