如何在rest api和yii2中从两个数据库表中将关系数据显示为json格式

时间:2015-08-19 09:59:29

标签: mysql angularjs rest yii2

我的数据库结构

部门表包含

  1. 部门标识
  2. 医生表包含

    1. 部门标识
    2. Doctor.php //型号名称

      public function fields() {
          return [
              'doctor_id',
              'd_name',
              'email',
              'phone',
              'speciality',
              'department' => function ($model) {
                  return $model->department->department; // Return related model property, correct according to your structure
              },
              'qualification',
              'profile_image',
              'experience',  
          ];
      }
      

      web.php

      [
          'class' => 'yii\rest\UrlRule',
          'controller' => ['doctor','department'],
      ],
      

      JSON数据

       [
          {
              "doctor_id":1,
               "d_name":"Saurabh Joshi",
               "email":"****@gmail.com",
               "phone":0,
               "speciality":"1",
               "department_id":"1",
               "qualification":"M.S. (General Surgery) – Marathwada University\n    MBBS – Marathwada University",
               "profile_image":"Saurabh_Joshi1-1439384488.jpg",
               "experience":""
          }
      ]
      

      当我从医生表中获取数据以在表单上显示时,我想要department name而不是department_id

0 个答案:

没有答案