Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[Newbie] ErrorException 4096 when trying to query the database
  • Hi, I'm totally new in using fuelphp, actually I'm a newbie at php programming, but i'm not new as being a programmer. Currently I need to create a web api for my mobile application, and I think fuelphp looks great for creating api, by utilizing its built in rest controller. Notes: I'm using fuelphp v1.5.3 on AMPPS on Mac.

    I started by using database table example from fuel installation (fuel_intro.messages) and created one model and one controller as follows:

    model/test.php

    namespace Model;

    class Test extends \Model
    {
    // Get All Messages
    public static function all_messages() 
    {
    $result = \DB::select('name', 'message')->from('messages')->execute();
    return $result;
    }
    }

    controller/test.php

    use \Model\Test;

    class Controller_Test extends Controller_Rest
    {
    // Return all messages data
    public function get_list() {
    $response = Test::all_messages();
    $this->response($response);
    }
    }

    I think that code is simple enough, however, when I run it through the browser (http://localhost/fueltest/public/test/list), it gives me the following error:

    "ErrorException [ 4096 ]: Object of class Fuel\Core\Database_Result_Cached could not be converted to string"

    What is wrong? I have tried with other table and the problem still the same.
    I try to dump the $result variable from model using print_r() command and it gives me the following output:
    "Fuel\Core\Database_Result_Cached Object ( [_query:protected] => SELECT `name`, `message` FROM `messages` [_result:protected] => Array ( [0] => Array ( [name] => Test01 [message] => Just another stupid message... ) ) [_total_rows:protected] => 1 [_current_row:protected] => 0 [_as_object:protected] => )"

    Any help to the right direction will be appreciated. Thanks in advance.

  • HarroHarro
    Accepted Answer
    When you set a response, the Reponse class will have to convert your response to a string to be able to echo it to the browser.

    The error message says you can't echo out a database response object. You can't echo any object, unless that object has __toString() implemented to convert the object to a string representation.

    This is a PHP thing...
  • Thanks Harro for the response. 

    What I need is to get the query result as json or xml so that my mobile app could use it. I thought by echoing out the response to the browser, it will show that the response is correct. I read somewhere in the documentation that by supplying different file extension e.g: json, xml, csv, etc... fuelphp will give the appropriate format as the response (default to config setting).

    Do you have any suggestion to make my previous code working? To make it able to send query result as json or xml to my mobile app? Thanks.
  • HarroHarro
    Accepted Answer
    If you want to build an API, have your controller extend Controller_Rest (for API only) or Controller_Hybrid (for a mix of REST and HTML output).

    It will take care of the output conversion for you (json, xml, etc). In this case the response need to be an array, so your DB query needs to return an array instead of an object (check the docs).
  • I've finally found the solution for my code above. In model/test.php I change 

    return $result;

    to

    return $result->as_array();

    and the code work as expected. Thanks.

Howdy, Stranger!

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

In this Discussion