Php 为什么不在Rest Yii2中显示关系数据
为什么不在Rest Yii2中显示关系数据 我有两张桌子 样本: 类别,子类别Php 为什么不在Rest Yii2中显示关系数据,php,rest,yii,yii2,yii-rest,Php,Rest,Yii,Yii2,Yii Rest,为什么不在Rest Yii2中显示关系数据 我有两张桌子 样本: 类别,子类别 <?php namespace app\controllers; use app\models\Category; use yii\web\NotFoundHttpException; use yii\web\Response; use yii\rest\Controller; class ApiController extends Controller { public functio
<?php
namespace app\controllers;
use app\models\Category;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\rest\Controller;
class ApiController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats'] = ['application/json' => Response::FORMAT_JSON];
return $behaviors;
}
public function actionGetSk($cId)
{
$result= Category::find()->with('subCategory')->where(['id' => $cId])->all()
return $result;
}
}
尝试以下方法:在您的
类别
模型中,添加以下方法:
public function extraFields() {
return [
'subcategory' => 'subCategory',
];
}
现在,使用expand
get参数调用api,如下所示:
http://yourapi.com/api/get-sk?cID=1&expand=subcategory
发布控制器声明并attributes@gmc如何发布??我的意思是你应该把代码添加到你已经发布的内容中,编辑你question@gmc编辑。如何修复此return$result是json结果,但在仅json的类别结果中,但在print\r($result)中,我有类别、子类别。为什么?
http://yourapi.com/api/get-sk?cID=1&expand=subcategory