Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting data as array instead of stdClass #1

Open
2 tasks
weierophinney opened this issue Dec 31, 2019 · 4 comments
Open
2 tasks

Getting data as array instead of stdClass #1

weierophinney opened this issue Dec 31, 2019 · 4 comments

Comments

@weierophinney
Copy link
Contributor

  • 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?


Originally posted by @kschroeer at zfcampus/zf-rest#114

@weierophinney
Copy link
Contributor Author

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);


Originally posted by @alextartan at zfcampus/zf-rest#114 (comment)

@weierophinney
Copy link
Contributor Author

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.


Originally posted by @TomHAnderson at zfcampus/zf-rest#114 (comment)

@weierophinney
Copy link
Contributor 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.


Originally posted by @kschroeer at zfcampus/zf-rest#114 (comment)

@weierophinney
Copy link
Contributor Author

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.


Originally posted by @weierophinney at zfcampus/zf-rest#114 (comment)

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

No branches or pull requests

1 participant