models/CallerIdentity.php에서와 같이 fields 함수를 재정의했습니다.Yii2 overidden fields()
public function fields()
{
$fields = parent::fields();
$fields[] = 'ehadata';
return $fields;
}
CallerIdentity는
public function getEhadata()
{
return $this->hasMany(EHAData::className(), ['cidref' => 'cidref']);
}
컨트롤러 클래스는 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": "[email protected]",
"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 ($ 필드) 출력을
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)
배열이 있음을 확인하려면하지만 난 상관없이 개별 필드 중 하나를 확인할 수 없습니다 무엇을 나는 노력한다. 이로 인해 업데이트 기능을 작성할 때 필드에 액세스하고 조작 할 수있는 방법에 대해 생각하게되었습니다. 누구 아이디어있어?
도움을 주시면 감사하겠습니다. 일반적
'비 객체의 속성을 얻으려고 시도했습니다 .' – Kyle
객체가 n 인 경우에만 isset()을 사용해야합니다. ot set – mrateb
업데이트 또는 테스트에 있습니까? – mrateb