获取包含 yii2 中所有相关数据的嵌套 json 对象

get nested json object containing all the related data in yii2

我正在写一个 api 来获取数据。我有三个模型 Page 、 Section 和 Question.This 是显示不同实体 之间关系的 erd 图的图像。

我已经在各个模型中定义了关系。

页面模型

class CmsPage extends ActiveRecord
{
    public static function tableName()
    {
         return '{{%cms_page}}';
    }

    public function getCmsSection()
    {
       /*Page and Section has many to many relation having a junction table*/
        return $this->hasMany(CmsSection::className(),['id'=>'section_id'])->viaTable('tbl_cms_page_section',['page_id'=>'id']);
    }
}

剖面模型

class CmsSection extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%cms_section}}';
    }

    public function getCmsQuestion()
    {
        return $this->hasMany(CmsQuestion::className(),['id'=>'section_id']);
    }

}

问题模型

class CmsQuestion extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%cms_question}}';
    }

    public function getCmsSection()
    {
        return $this->hasOne(CmsSection::className(),['section_id'=>'id']);
    }
}

我想将数据作为 json 对象获取,如下所示

{
"id": "1",
"name": "Lorem Ipsom",
"title": "Eiusmod voluptate Lorem aute tempor fugiat eiusmod ex ipsum enim magna consequat cupidatat.",
"short_summary": "Will ContentsQuis consequat occaecat consequat aliquip adipisicing aute sunt pariatur culpa sint consectetur reprehenderit eu aliquip.",
"summary": "Officia eu fugiat quis laboris voluptate sunt sint cupidatat eu consequat et deserunt sint eu.",
"section": [
  {
    "id": 1,
    "title": "Advanced Planning",
    "short_summary": "Advanced Planning",
    "summary": "Summary ",
    "question": [
      {
        "id": 1,
        "question_type_id": 1,


        "show_relations": "true",
        "title": "Family Members",
        "place_holder_text": "Your faimly list is currently empty.",

        "label_name": "Name",
        "label_email": "Email",
        "label_phone": "Mobile number",
        "select_1": "url:test-data/dashboard-relationship.json",
        "label": "Remarks"

      },
      {
        "id": 2,
        "question_type_id": 2,


        "title": "Close Friends",
        "place_holder_text": "Your list is close friends currently empty.",

        "label_name": "Name",
        "label_email": "Email",
        "label_phone": "Mobile number",
        "label_address": "Address",
        "help_text": "<b>Close Friends<b><ul><li>1</</li><li>2</</li></ul>"
      },
      {
        "id": 3,
        "question_type_id": 3,

        "title": "Designated Executors",
        "place_holder_text": "It is vital you nominate an Executor",

        "label_name": "Name",
        "label_email": "Email",
        "label_phone": "Mobile number",
        "label_address": "Address"
      },
      {
        "id": 4,
        "question_type_id": 4,
        "question_type": {},
        "title": "Witnesses",
        "place_holder_text": " It is vital you nominate an Executor",
        "label_1": "Name",
        "opption_2": "Email",
        "label_3": "Phone",
        "label_4": "Occupation",
        "label": "Address"
      }
    ]
  },
  {
    "id": 2,
    "title": "Labore proident cupidatat ex dolore occaecat in tempor sit proident sint labore minim cillum.",
    "summary": "Dolor cupidatat consequat cillum deserunt laborum aliqua commodo occaecat sint aute cillum exercitation.",
    "short_summary": "Ullamco Lorem incididunt dolore ipsum.",
    "question": [
      {

        "id": 5,
        "question_type_id": 5,
        "title": "Last Words",
        "short_summary": "Plan, the way you want!",
        "summary": "Start by writing down.",
        "label": "Write your last words here"
      },
      {

        "id": 6,
        "question_type_id": 5,
        "title": "Venue",
        "short_summary": "Plan, the way you want!",
        "summary": "Start by writing down.",
        "label": "Mention"
      }

    ]
  }

]

}

我跟随 yii2 docs 走到了这一步,但无法获取 json 格式的数据。怎么做?

HI 在 API 中有一个函数调用字段,您可以覆盖该函数:

请检查下面的代码,这将帮助您了解或了解更多详细信息 你在这里阅读 yii2 文档 Yii 2 fields doc

public function fields()
{
    return [
        'id',
        "name"
        'title',
        'short_summary',
        'summary',
        'section'=> function ($model) {
            return $model->cmsSection;
        }, 
    ];
}

如果你想嵌套得更多,那么你可以先使用数组存储嵌套数据,然后再对内部数据使用循环:

'section'=> function ($model) {
                 $sections =  $model->cmsSection;
                   //store section data in array
                 //forloop of $sections
                  $sections->question;
                //Again store question data in first array and return it  
            }, 

应用它你就会明白我想告诉你的。