Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Getting data as array instead of stdClass #114

Open
2 tasks
kschroeer opened this issue May 28, 2018 · 5 comments
Open
2 tasks

Getting data as array instead of stdClass #114

kschroeer opened this issue May 28, 2018 · 5 comments

Comments

@kschroeer
Copy link

  • I was not able to find an open or closed issue matching what I'm seeing.
  • This is not a question. (Questions should be asked on chat (Signup here) or our forums.)

Provide a narrative description of what you are trying to accomplish.

Code to reproduce the issue

Expected results

Actual results

I'm using zf-rest for building my RESTful web apps. I have the following configurations:

'zf-rest' => [
  Resource\FeedbackResource::class => [
    'listener' => Resource\FeedbackResource::class,
    'route_name' => 'api/rest/feedback',
    'entity_http_methods' => [
    ],
    'collection_http_methods' => [
      'POST',
    ],
  ],
],
'zf-content-validation' => [
  Resource\FeedbackResource::class => [
    'use_raw_data' => false,
    'allows_only_fields_in_filter' => true,
    'POST' => Resource\FeedbackResource::class . '\\Validator',
  ],
],
'input_filter_specs' => [
  Resource\FeedbackResource::class . '\\Validator' => [
    Resource\FeedbackResource::PARAM_NAME => $inputFilterSpecForStrings,
    Resource\FeedbackResource::PARAM_EMAIL => $inputFilterSpecForStrings,
  ],
],

Then I created the resource with the corresponding method:

class FeedbackResource extends AbstractResourceListener
{
  public function create($data)
  {
    // do something
  }
}

I posted a json string to the endpoint and everything works fine so far. But what I'm wondering about is that I will get $data as an object with the json data as attributes. I expected to get an assoziative array. Is this possible?

@alextartan
Copy link

alextartan commented Jun 20, 2018

you can always cast it to an array. like: $dataArray = (array)$data; assuming you don't have a nested object. in which case, you can do $dataArray = json_decode(json_encode($data), true);

@TomHAnderson
Copy link
Contributor

You can pass an object around and change the properties. An array (not ArrayObject) cannot do that and I believe that's why $data is an object.

@kschroeer
Copy link
Author

Yes, I know I can use (array), get_object_vars or json_encode/json_decode, I was only wondering about the fact that fetchAll() always gets an array (GET request) and create() gets an object instead (POST request). That seems a bit unlogic.

@weierophinney
Copy link
Member

I was only wondering about the fact that fetchAll() always gets an array (GET request) and create() gets an object instead (POST request). That seems a bit unlogic.

How so? A GET request has no request body, so any parameters passed are query string arguments; these generally do not map to an object, but are rather considered key/value pairs that typically are not related. As such, we do not map them to objects.

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools-rest; a new issue has been opened at laminas-api-tools/api-tools-rest#1.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants