在rest api yii2中设置extraFields

时间:2017-12-05 22:06:14

标签: rest yii2

public function fields()
{
    return [
        'field' => 'field',
    ];
}

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

返回:

{ "field": "field", "users": { "id": 1, "name": "user" } }

如何排除ID?

public function extraFields()
{
    return [
        'users' => function($model){
            return [
                'name' => $model->users->name,
            ];
        }
    ];
}

返回:

{ "field": "field", "users": { "name": null } }

如何正确填写名称字段或如何自定义字段输出过滤?

1 个答案:

答案 0 :(得分:0)

选项可以覆盖用户模型中的field()方法:

public function fields()
{
    $fields = parent::fields();
    if ($something) {
        unset($fields['id']);
    }

    return $fields;
}