Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

rest - Yii2 RESTful relational data

I am having some trouble getting the Yii2 RESTful API returning relational data. I have this working when viewed through the frontend but i am trying to get the same data through the API and its not working the same way.

Tables
country - PK is population_id
population - Foreign Key is country.population_id

I am getting this error:

{ "success": false, "data": { "name": "Invalid Configuration", "message": "The "query" property must be an instance of a class that implements the QueryInterface e.g. yiidbQuery or its subclasses.", "code": 0, "type": "yiiaseInvalidConfigException", "file": "C:xampphtdocsAdvancedAPIvendoryiisoftyii2dataActiveDataProvider.php", "line": 100, "stack-trace": [ "#0 C:xampphtdocsAdvancedAPIvendoryiisoftyii2dataBaseDataProvider.php(79): yiidataActiveDataProvider->prepareModels()", "#1 C:xampphtdocsAdvancedAPIvendoryiisoftyii2dataBaseDataProvider.php(92): yiidataBaseDataProvider->prepare()", "#2 C:xampphtdocsAdvancedAPIvendoryiisoftyii2 estSerializer.php(162): yiidataBaseDataProvider->getModels()", "#3 C:xampphtdocsAdvancedAPIvendoryiisoftyii2 estSerializer.php(131): yii estSerializer->serializeDataProvider(Object(yiidataActiveDataProvider))", "#4 C:xampphtdocsAdvancedAPIvendoryiisoftyii2 estController.php(97): yii estSerializer->serialize(Object(yiidataActiveDataProvider))", "#5 C:xampphtdocsAdvancedAPIvendoryiisoftyii2 estController.php(75): yii estController->serializeData(Object(yiidataActiveDataProvider))", "#6 C:xampphtdocsAdvancedAPIvendoryiisoftyii2aseController.php(153): yii estController->afterAction(Object(yiiaseInlineAction), Object(yiidataActiveDataProvider))", "#7 C:xampphtdocsAdvancedAPIvendoryiisoftyii2aseModule.php(455): yiiaseController->runAction('index', Array)", "#8 C:xampphtdocsAdvancedAPIvendoryiisoftyii2webApplication.php(83): yiiaseModule->runAction('v1/country/inde...', Array)", "#9 C:xampphtdocsAdvancedAPIvendoryiisoftyii2aseApplication.php(375): yiiwebApplication->handleRequest(Object(yiiwebRequest))", "#10 C:xampphtdocsAdvancedAPIapiwebindex.php(17): yiiaseApplication->run()", "#11 {main}" ] } }

model (Country.php):

<?php
namespace apimodulesv1models;
use yiidbActiveRecord;

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 apimodulesv1models;
use yiidbActiveRecord;

class Population extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'population';
    }

    /**
     * @inheritdoc
     */
    public static function primaryKey()
    {
        return ['p_id'];
    }

}

controller (CountryController.php):

<?php

namespace apimodulesv1controllers;

use yii
estController;
use yiidataActiveDataProvider;
use apimodulesv1modelsCountry;

class CountryController extends Controller
{

    public function actionIndex()
    {
        $query = Country::find()->with('countries', 'populationNumber')->all();
        //$query = Country::find();

        return new ActiveDataProvider([
            'query' => $query,
        ]);
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to remove all() part from your query. So the code should be:

<?php
namespace apimodulesv1controllers;

use yii
estController;
use yiidataActiveDataProvider;
use apimodulesv1modelsCountry;

class CountryController extends Controller
{
    public function actionIndex()
    {
        $query = Country::find()->with('countries', 'populationNumber');
        //$query = Country::find();

        return new ActiveDataProvider([
            'query' => $query,
        ]);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...