Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is there a way to get a standard array of results from any ORM query?
  • Is there a way to use a method similar to ->as_array() on an ORM object that returns just the results, and not all the associated ORM information? Debugging is much easier and faster when you're presented with just the information you requested from the DB. Along with the ability to pass items to the REST controllers and output them using the format class easier. I might be being an idiot, and there is already a way to do this, but as far as i'm aware, there isn't. Cheers,
    Dan.
  • There's 2 ways you might do this: - Do it using method chaining and end with ->get_query(), that returns the query to you as a Query Builder object and you can add ->execute()->as_array() yourself. You won't like the result much though as it aliasses all the columns.
    - You can convert each individual model instance (and it's relations) into an array using $obj->to_array().
  • Thanks Jelmer, i get an error when attempting this in the oil console though: if i do: $sites_list = Model_Site::find('all'); // Find all records from the 'sites' table.
    $sites_array = $sites_list->to_array(); I get the following error:
    "PHP Fatal error: Call to a member function to_array() on a non-object in /Users/danielmatthews/Sites/statusboard/fuel/packages/oil/classes/console.php(91) : eval()'d code on line 1" I assume this is because what ::find('all') returns is not an instance of an object but an array of results. So what's the best method to produce an object that ->to_array() will work on? Thankyou for your help, it's really appreciated.
  • foreach ($result as $i => $obj)
    {
        $result[$i] = $obj->to_array();
    }
    

    Bottom line is: the ORM returns objects, that's what it's about.
  • Oh ok, so that extra step of converting it using a loop needs to be done, this is what i was trying to avoid. Could i put in a feature request to perhaps add a ->get_array(); method that does this for you for instances of ORM Models? Is that something that i should post to the github issue queue? Obviously it moves data out of the ORM context, but it does allow you to print much cleaner output for debugging and parsing into other formats. Thanks again.
  • The ORM was never meant for this, as I said an ORM works with objects. I won't spend any time on implementing it, but I will consider any feature request seriously. Though if it slows down hydration even so slightly it won't get in. Things like this should do what they're supposed to and not complicate unnecessarily. That slows it down for everyone when being a benefit to only some, which is not a worthwhile tradeoff. And adding a method that just does this loop natively is useless IMO, it only hides what happens while saving hardly any lines of code.
  • Okay, i think that's a fair argument against. Thanks for the help Jelmer, appreciate it. Dan.

Howdy, Stranger!

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

In this Discussion