2017-11-15 6 views
0

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']); 
} 

ControllerクラスはNumbersControllerです。ですから、私がapi.dev/v1/numberに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) 

、どんな配列があることを確認していないために、私は、個々のフィールドのいずれかを確認することができません私は試してみる。これは、私が更新関数を書くときに、フィールドにどのようにアクセス/操作することができるかを考えさせました。誰にもアイデアはありますか?

大変お手伝いをいたします。一般に

答えて

0

、あなたがフィールドを(上書きする場合)として:

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

return $fields; 
} 

$model->ehadataオブジェクト

例の配列であるので、次に、フィールドにアクセスすることは、例えば$model->ehadataをループを介して、容易になります。

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

これを試して、 '非オブジェクトのプロパティを取得しようとしました.' – Kyle

+0

オブジェクトがnの場合にのみ、isset()を使用する必要があります。 ot set – mrateb

+0

これはアップデート中かテスト中ですか? – mrateb