我在models / CallerIdentity.php中重写了字段函数。
public function fields()
{
$fields = parent::fields();
$fields[] = 'ehadata';
return $fields;
}
CallerIdentity有这种关系......
public function getEhadata()
{
return $this->hasMany(EHAData::className(), ['cidref' => 'cidref']);
}
Controller类是NumbersController。所以现在如果我向api.dev/v1/numbers发出GET请求,响应是这样的,这就是我的目标。
{
"cidref": 411,
"custref": 178,
"caller_id": "978378478",
"expiry": "2021-06-27",
"conf_call": "n",
"type": "F",
"redirect": null,
"destination": "help@help.com",
"status": 1,
"start_date": "2010-09-17",
"last_polled": "2012-12-07 08:30:02",
"ehadata": [
{
"status": 0,
"name": "Blah ",
"bussuffix": "Ltd",
"premesis": "Blah House",
"thoroughfare": "Blah Road",
"locality": "Manchester",
"postcode": "T56 T4G"
}
]
},
来为端点编写测试,我无法访问任何ehadata字段。
我能做到:
$I->seeResponseJsonMatchesJsonPath('$[0].ehadata');
va_dump($ fields)输出
array (size=12)
'cidref' => string 'cidref' (length=6)
'custref' => string 'custref' (length=7)
'caller_id' => string 'caller_id' (length=9)
'expiry' => string 'expiry' (length=6)
'conf_call' => string 'conf_call' (length=9)
'type' => string 'type' (length=4)
'redirect' => string 'redirect' (length=8)
'destination' => string 'destination' (length=11)
'status' => string 'status' (length=6)
'start_date' => string 'start_date' (length=10)
'last_polled' => string 'last_polled' (length=11)
1 => string 'ehadata' (length=7)
要检查阵列是否存在,但无论我尝试什么,我都无法检查任何单个字段。这让我想到当我来编写更新功能时,如何访问/操作字段?有没有人有任何想法?
任何帮助都会受到大力赞赏。
答案 0 :(得分:0)
通常,如果将fields()覆盖为:
public function fields()
{
$fields = parent::fields();
$fields['ehadata'] = 'ehadata';
return $fields;
}
然后通过$model->ehadata
循环访问字段很容易,因为$model->ehadata
是一个对象数组
示例:
foreach($model->ehadata as $temp)
{
$temp->status = 2;
}