Я пишу API для получения данных. У меня есть три модели Страница, Раздел и Вопрос. Это изображение диаграммы ЭРД, показывающее отношения между различными объектами .
Я определил там связи в соответствующих моделях.
Модель страницы
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 документами , чтобы прийти к этому далеко, но не смог получить данные в формате json. Как это сделать?