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

REST controller with json - empty output! #29

Closed
dhrrgn opened this issue Apr 13, 2011 · 4 comments
Closed

REST controller with json - empty output! #29

dhrrgn opened this issue Apr 13, 2011 · 4 comments
Assignees
Labels
Milestone

Comments

@dhrrgn
Copy link
Contributor

dhrrgn commented Apr 13, 2011

There's a bug with JSON output in a REST controller. Here's some code:

class Controller_API extends Controller_Rest {
    public function get_list()
    {
        $users = Model_User::find('all');
        //print_r($users)
        $this->response($users);
    }
}

This will output:

{ 1: { } 2: { } }

I have two users in my test environment, so having 2 json values would be correct, but they're empty.
By uncommenting the print_r I get my 2 results so the $users variable is correct.

Also, if I change config/rest.php back to xml, it outputs everything correctly.
More details here: http://fuelphp.com/forums/topics/view/974

@ghost ghost assigned philsturgeon Apr 13, 2011
@dhrrgn
Copy link
Contributor Author

dhrrgn commented Apr 14, 2011

Making this a Bug. It was submitted by koichi h Today.

@koichirose
Copy link

Commenting to be notified of changes.

@jschreuder
Copy link
Contributor

I'm speculating here but I think the problem is this line: https://github.com/fuel/core/blob/develop/classes/format.php#L215

My assumption is that json_encode() will run a get_object_vars() on the Model_User object, which will return an empty array as all the properties are protected or private. The way this could be solved is by doing something like this:

// Encode as JSON
public function to_json()
{
    $data = $this->_data instanceof ArrayAccess ? $this->to_array($this->_data) : $this->_data;
    return json_encode($data);
}

Or do this by converting objects to arrays with a _from...() method that can use either get_object_vars() or the above solution.

@jschreuder
Copy link
Contributor

As it currently doesn't work at all I submitted my fix, don't have the time to properly test it though.

benharold pushed a commit to benharold/core that referenced this issue Oct 21, 2013
Fix issue where controller generation was always singularizing class name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants