簡體   English   中英

Yii2覆蓋字段()

[英]Yii2 overidden fields()

我已經在models / CallerIdentity.php中覆蓋了字段功能。

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": "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)

要檢查該數組是否存在,但是無論嘗試如何,都無法檢查任何單個字段。 這使我開始思考如何編寫更新功能,如何訪問/操作這些字段? 有人知道嗎?

任何幫助將不勝感激。

通常,如果將fields()重寫為:

public function fields()
{
  $fields = parent::fields();
  $fields['ehadata'] = 'ehadata';

 return $fields;
} 

然后通過遍歷$model->ehadata訪問字段,因為$model->ehadata是對象數組

例:

foreach($model->ehadata as $temp)
{
   $temp->status = 2;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM