fields() function to grab and display related data.


(Kyle) #1

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=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='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.


(Alirz23) #2

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=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='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.yiiframework.com/doc-2.0/guide-rest-resources.html#overriding-extra-fields


(Kyle) #3

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’


(Alirz23) #4

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