Search code examples
phprestyii2relational-database

Yii2 REST API relational data return


I've set up Yii2 REST API with custom actions and everything is working just fine. However, what I'm trying to do is return some data from the API which would include database relations set by foreign keys. The relations are there and they are actually working correctly. Here's an example query in one of the controllers:

$result = \app\models\Person::find()->joinWith('fKCountry', true)
      ->where(..some condition..)->one();

Still in the controller, I can, for example, call something like this:

$result->fKCountry->name

and it would display the appropriate name as the relation is working. So far so good, but as soon as I return the result return $result; which is received from the API clients, the fkCountry is gone and I have no way to access the name mentioned above. The only thing that remains is the value of the foreign key that points to the country table.

I can provide more code and information but I think that's enough to describe the issue. How can I encode the information from the joined data in the return so that the API clients have access to it as well?


Solution

  • Set it up like this

    public function actionYourAction() {
        return new ActiveDataProvider([
            'query' => Person::find()->with('fKCountry'), // and the where() part, etc.
        ]);
    }
    

    Make sure that in your Person model the extraFields function includes fKCountry. If you haven't implemented the extraFields function yet, add it:

    public function extraFields() {
        return ['fKCountry'];
    }
    

    and then when you call the url make sure you add the expand param to tell the action you want to include the fkCountry data. So something like:

    /yourcontroller/your-action?expand=fKCountry