Yii2休息额外的字段

时间:2017-10-19 10:29:07

标签: php rest api yii2

使用GET列出记录时,包含的extraField显示正确的值 - rate_increase是额外的字段。

class ResellerRateResource extends ResellerRate
{
public $rate_increase;
/*
 * @return $fields array Filtered fields for API presentation
 */
public function fields()
{
    $fields = array_merge(parent::fields(), ['rate_increase']);

    return $fields;
}

模型

 $query = $model::find()
                ->leftJoin('reseller_config', 'reseller_rate.resellerref = reseller_config.resellerref')
                ->select('reseller_rate.*, reseller_config.rate_increase AS rate_increase');

查询以获取数据:

{{1}}

但是,在查看单个记录时,它始终显示为null。任何人都知道为什么?

2 个答案:

答案 0 :(得分:0)

我很确定这条线是错误的:

$fields = array_merge(parent::fields(), ['rate_increase']);

什么是['rate_increase']?您刚刚定义的数组,包含字符串值。

我认为你需要一些$variable['rate_increase']然后它可能会有效。

答案 1 :(得分:0)

据我所知fields仅用于查看隐藏敏感信息。但是,如果您的自定义查询中有任何动态属性,则可以使用属性函数来获取属性的值:

function attributes() {
   $attrs = parent::attributes(); // get all the attributes of the current model

   $attrs[] = 'rate_increase';
   return $attrs;

}

现在,如果您在rate_increase列中有值,则会得到以下值:$model->rate_increase;

http://www.yiiframework.com/doc-2.0/yii-base-model.html#attributes()-detail