You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
There's a bug with JSON output in a REST controller. Here's some code:
This will output:
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
The text was updated successfully, but these errors were encountered: