简体   繁体   中英

Yii restful API, how to modify return results from model

I followed this tutorial to set up my back end server based on Yii framework and then this tutorial to set up my API and everything is working as it should. But I am not sure how to accomplish the next step: My return array right now is pulling records that look like this:

{
 "id": 1,
 "user_id": 1,
 "description": "This is a summary of article 1",
 "status": 2,
 "type": 1,
 "created_at": 1426210780,
 "updated_at": 1426365319
}

The value for 'type' is '1' in the db, but I want to store all the possible values of 'type' in another table like this:

 1 : Red
 2 : Blue
 3 : Green

And then I want the JSON returned via my API to contain "type":'red' instead of "type":1. I assume I need to override something in my Model, but I can't figure out what to override or with what.

I'm happy to read through tutorials or documentation but I'm such a beginner at this that I'm not sure what terms to search for. Thanks for your help!

Have a look at models and their relationships to other models, this will allow you to get the information you need.

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Once the relationship is working correctly you should be able to get the colour from the original model.

Although this is from a earlier version of Yii it may help you understand how the models will interact as well http://www.yiiframework.com/wiki/285/accessing-data-in-a-join-table-with-the-related-models/

@Burrito's response provided documentation but I want to give the full solution for other searchers:

First, I needed to set up a model for 'Type'.

Second, I needed to declare the relationship between 'Report' (my main model) and 'Type' like this (in my Report model):

public function getType()
{
    return $this->hasOne(Type::className(), ['id' => 'type']);
}

(I'm not sure if that step is necessary, but the documentation makes it seem necessary.)

Third, I created getTypeName (in Report model), to get the name of the type based on the ID:

public function getTypeName($type = null)
{
    return Type::findOne($type)->name;
}

Lastly, in my apiController, I modified the function that I am using to get all records to include a loop for each record that called getTypeName:

protected function findAllReports()
{
    // get all reports
    $reports = Report::find()
                ->asArray()
                ->all();

    if( $reports ){
        $i = 0; 
        // loop through each report
        foreach($reports as $report){
            $model = new Report();
            // add a new key/value pair to each report array, populate with getTypeName and pass the type ID to it as a parameter
            $reports[$i]['typeName'] = $model->getTypeName($reports[$i]['type']);
            $i++;
        }
        return $reports;
    } else {
       // error or no results
    }
}

For reference, the other routine needed here is the action that the API hits, which calls findAllReports():

public function actionList()
{
    $reports=$this->findAllReports();
    $this->setHeader(200);
    echo json_encode(array('status'=>1,'data'=>$reports),JSON_PRETTY_PRINT);
}

Finally, now if I called [url]/api/list, I get an array of reports, including the typeName.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM