我在使用Yii2 RESTful API返回关系数据时遇到了一些麻烦。我通过前端查看时有这个工作,但我试图通过API获取相同的数据,但它的工作方式不同。
表
country
- PK是population_id
population
- 外键是country.population_id
我收到此错误:
{“success”:false,“data”:{“name”:“无效配置”, “message”:“\”查询\“属性必须是类的实例 实现QueryInterface,例如yii \ db \ Query或其子类。“, “code”:0,“type”:“yii \ base \ InvalidConfigException”,“file”: “C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \数据\ ActiveDataProvider.php”, “line”:100,“stack-trace”:[“#0 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \数据\ BaseDataProvider.php(79): yii \ data \ ActiveDataProvider-> prepareModels()“,”#1 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \数据\ BaseDataProvider.php(92): yii \ data \ BaseDataProvider-> prepare()“,”#2 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \其余\ Serializer.php(162): yii \ data \ BaseDataProvider-> getModels()“,”#3 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \其余\ Serializer.php(131): YII \其余\ Serializer-> serializeDataProvider(对象(YII \数据\ ActiveDataProvider))”, “#4 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \其余\ Controller.php这样(97): YII \其余\ Serializer->序列化(对象(YII \数据\ ActiveDataProvider))”, “#5 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \其余\ Controller.php这样(75): YII \其余\控制器 - > serializeData(对象(YII \数据\ ActiveDataProvider))”, “#6 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \碱\ Controller.php这样(153): YII \其余\控制器 - > afterAction(对象(YII \基\ InlineAction), 对象(yii \ data \ ActiveDataProvider))“,”#7 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \碱\ Module.php(455): yii \ base \ Controller-> runAction('index',Array)“,”#8 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \幅\ Application.php(83): yii \ base \ Module-> runAction('v1 / country / inde ...',Array)“,”#9 C:\ XAMPP \ htdocs中\ AdvancedAPI \厂商\ yiisoft \ yii2 \碱\ Application.php(375): yii \ web \ Application-> handleRequest(Object(yii \ web \ Request))“,”#10 C:\ XAMPP \ htdocs中\ AdvancedAPI \ API \网络\的index.php(17): yii \ base \ Application-> run()“,”#11 {main}“]}}
model(Country.php):
<?php
namespace api\modules\v1\models;
use \yii\db\ActiveRecord;
class Country extends ActiveRecord
{
public static function tableName()
{
return 'country';
}
public function getCountries()
{
//return $this->hasMany(Population::className(), ['population_id' => 'population_id']);
return $this->hasMany(Country::className(), ['population_id' => 'population_id']);
}
public function getPopulationNumber()
{
//return $this->hasOne(Country::className(), ['population_id' => 'population_id']);
return $this->hasOne(Population::className(), ['population_id' => 'population_id']);
}
}
model(Population.php):
<?php
namespace api\modules\v1\models;
use \yii\db\ActiveRecord;
class Population extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'population';
}
/**
* @inheritdoc
*/
public static function primaryKey()
{
return ['p_id'];
}
}
controller(CountryController.php):
<?php
namespace api\modules\v1\controllers;
use yii\rest\Controller;
use yii\data\ActiveDataProvider;
use api\modules\v1\models\Country;
class CountryController extends Controller
{
public function actionIndex()
{
$query = Country::find()->with('countries', 'populationNumber')->all();
//$query = Country::find();
return new ActiveDataProvider([
'query' => $query,
]);
}
}
答案 0 :(得分:2)
您需要从查询中删除all()
部分。所以代码应该是:
<?php
namespace api\modules\v1\controllers;
use yii\rest\Controller;
use yii\data\ActiveDataProvider;
use api\modules\v1\models\Country;
class CountryController extends Controller
{
public function actionIndex()
{
$query = Country::find()->with('countries', 'populationNumber');
//$query = Country::find();
return new ActiveDataProvider([
'query' => $query,
]);
}
}