Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Responding DB result set in a REST controller
  • Hi, when I try to respond with a DB result set from a REST controller the JSON data just shows empty entries. I see that the DB result basically gives me an array with class instances, which is why json can't encode it. Thing is I'd expect the your REST controller to automatically take care of this. I can't find any documentation on how I'm expected to take care of this though, can anyone clarify, how does FuelPHP expect me to respond with a DB result set through REST? I know I can iterate through the results myself and build the response object, but I imagine FuelPHP has something in place for this.
  • Use as_array() to get the result as an array instead of as objects.
  • That only works on the individual objects, I'd still need to iterate through the array to get the as_array variant. I would hope there is a way to do this with one function call thats supported by the framework rather than to have to make my own function for it. To be clear, it's very easy to do this myself, it's just that you'd expect the framework to have the tools available to do this. And when it uses its own REST controller, you'd expect this REST controller to be able to handle a common "problem" like this automatically. For now I've extended the REST controller myself to fabricate support for responding with a DB result set (as well as just using "return $x" rather than "$this->response($x)"), but I'd like to see native support. (I have no idea where that wink emoticon is coming from)
  • Yes that would require me to manually loop through the array. Like I said I know I can do this and am already doing this. It just seems weird to me that the framework requires me to do this when it would be so easy for the framework to offer a build in solution. I mean after all I use a framework to make these type of things automatic, so that I don't have to write useless code like this myself. All I wanted to know by means of this thread is whether there was a framework provided solution for this, but it seems there is not. Thanks anyway, hope to see these type of things improve with future versions.
  • Did you actually read Dan's response to that question?
    $query = \DB::select( 'username' )
               ->from( 'users' )
               ->execute()
               ->as_array(null, 'username');
    
    would return an array of username's. No iteration needed.
    * Return all of the rows in the result as an array.
      *
      *     // Indexed array of all rows
      *     $rows = $result->as_array();
      *
      *     // Associative array of rows by "id"
      *     $rows = $result->as_array('id');
      *
      *     // Associative array of rows, "id" => "name"
      *     $rows = $result->as_array('id', 'name');
    
  • That requires you to execute your queries a certain way though, and would not work (for example) on a Model > find() query.
  • That is correct, the ORM is currently not designed to produce results for 'batch' type of processing. It is absolutely not a must to use the ORM for every interaction with the database, choose the method that suits the requirement. Nobody's stopping you from adding a method to your model that uses a DB::select() to return data in a format that your application requires.
  • I know that, I was simply wondering what the framework had to offer and suggesting that offering some type of native support for something so "everyday" would really compliment the framework. As a first timer, the longer you can work with the framework without being forced to come up with your own code to address it's shortcomings, the better.
  • Please add a feature request at http://github.com/fuel/orm/issues, so it can be looked at and it's status tracked. An issue not created is an issue that doesn't exist... ;)
  • Thanks, I've done as you suggested :)

Howdy, Stranger!

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

In this Discussion