Yii Framework Forum: fields() function to grab and display related data. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

fields() function to grab and display related data.

#1 User is offline   Kyle96 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 25-July 17

Posted 15 November 2017 - 09:22 AM

Bare with me as I'm not the best at articulating...

I have overridden the fields function like so in models/CallerIdentity.php.
  
public function fields()
{
   $fields = parent::fields();
   $fields[] = 'ehadata';

   return $fields;
} 


CallerIdentity has relation set like so:

  
public function getEhadata()
{
   return $this->hasMany(EHAData::className(), ['cidref' => 'cidref']);
}


Controller class is NumbersController. So now if I fire off GET request to api.dev/v1/numbers the response is like so, which is what I was aiming for.

 {
        "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"
            }
        ]
    },


Come to writing tests for the endpoint and I cannot access any of the ehadata fields.

I can do:
$I->seeResponseJsonMatchesJsonPath('$[0].ehadata');


var_dump($fields) output
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)



To check that the array is there but I can't check any of the individual fields, no matter what I try. This made me think about when I come to write the update function, how can I access/manipulate the fields?


Any help would be massively appreciated.
0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,492
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 15 November 2017 - 11:02 AM

Hi there,

What I can guess from looking at the code and var_dump, all your column names are string keys but the last key is an integer look at the key is 1, which should be a string.

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)


also I would recommend looking at the the extraFields method for adding relationships to response instead of overloading fields method.

here is a reference to the docs/guides.

http://www.yiiframew...ng-extra-fields
0

#3 User is offline   Kyle96 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 25-July 17

Posted 15 November 2017 - 11:11 AM

I did implement it first with extraFields() however having to type ?expand=ehadata is an annoyance and i think users would prefer the data to be right there in front of them. I have had the key as a string and that didnt make a difference.

What i dont get is when var_dumping $fields it returns

 1 => string 'ehadata' (length=7)


or the way I originally had it

 'ehadata' => string 'ehadata' (length=7)


but the response in json returns

 "ehadata": [
            {
                "status": 0,
                "name": "Blah ",
                "bussuffix": "Ltd",
                "premesis": "Blah House",
                "thoroughfare": "Blah Road",
                "locality": "Manchester",
                "postcode": "T56 T4G"
            }
        ]


I thought I would be able to access the fields like

$model->ehadata['name'];


or

$fields->ehadata['name'];


but they both return 'Trying to get property of non-object'
0

#4 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,492
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 15 November 2017 - 11:56 AM

another thing I noticed is ehadata is an array you need to access one of the index, change your selector to '$[0].ehadata[0]'

also i would recommend change the key to be string instead of integers
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users