代码之家  ›  专栏  ›  技术社区  ›  umair.ashfr

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

  •  0
  • umair.ashfr  · 技术社区  · 4 年前

    我正在编写一个api来获取数据。我有三个模型页、节和问题。这是显示不同实体之间关系的erd图的图像 This is image of the erd diagram showing relationship between different entities

    我在各自的模型中定义了这些关系。

    页面模型

    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']);
        }
    }
    

    {
    "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

    0 回复  |  直到 4 年前
        1
  •  1
  •   Amitesh Kumar    4 年前

    嗨,在API中,有一个函数调用字段可以重写该函数:

    请检查下面的代码,这将帮助您获得想法或了解更多详细信息 你在这里读到了yii2 doc 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  
                },