Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
JSON format Bug?
  • hi guys,
    I have created a REST Controller and extracted data from a db table. There are 2 things :
    1- In core\classes\format.php there's still the array bug ( http://fuelphp.com/forums/topics/view/974/10 )
    2- After the correction and to_array() workaround, with Orm I obtain a wrong formatted JSON output Using DB Class I've right JSON formatted data :
    public function get_list()
    {
        $resellers = DB::select('*')->from('resellers');
        if(Input::get('id')) $resellers->where('id', '=', Input::get('id'));
        if(Input::get('name')) $resellers->where('nome', '=', Input::get('name'));
        if(Input::get('surname')) $resellers->where('cognome', '=', Input::get('surname'));
        if(Input::get('company')) $resellers->where('azienda', 'like', '%'.Input::get('company').'%');
        $result = $resellers->execute();
            
        $this->response($result);
    }
    
    [{"id":"25","nome":"Name1","cognome":"Surname1","azienda":"Company1"}, "id":"26","nome":"Name2","cognome":"Surname2","azienda":"Company2"}]
    

    With Orm :
    public function get_list()
    {
        $resellers = \Model_Reseller::find('all');     
        $result = Format::factory($resellers)->to_array();
        $this->response($result);
    }
    
    {"25":{"id":"25","nome":"Name1","cognome":"Surname1","azienda":"Company1"},"26": "id":"26","nome":"Name2","cognome":"Surname2","azienda":"Company2"}}
    

    Where are "[" and "]"?? Where I'm wrong?? Thanks!
  • The first output is an array (thus enclosed in square brackets), the second is an object (thus enclosed in curly brackets). That is because it uses the IDs as index instead of just enumerating the output. And to be clear: not a bug, all this looks perfectly fine. [EDIT] On another note, this belongs in the general discussion because you're asking about the format class. Using the Orm with it doesn't make this about the Orm.
  • Ok thank you Jelmer, nice as always. Sorry for my error...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion