Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Noob question: getting results from DB::query as object
  • In my ViewModel I have something like $sql = 'Select id From mytable';
    $this->rows = DB::query($sql)->execute()->as_array(); And in the view I have something like <ul>
    <? foreach ($rows as $row): ?>
    <li><?= $row ?></li>
    <? endforeach ?>
    </ul> How can I return the results from DB::query() as objects, so that my View can have <?= $row->id ?> Thanks, Chris
  • Use as_object() instead of as_array() ?
  • Thanks for the suggestion, but it gives me "ErrorException [ Error ]: Call to undefined method Fuel\Core\Database_Result_Cached::as_object()".
  • as_object() should come before execute(), not after it.
  • Thanks for taking the time to respond (especially as it looks like you've been in the middle of switching to a new forum!), but I had tried putting as_object()->execute() – it just gives "Fuel\Core\FuelException [ Error ]: Database results are read-only"...
  • This is the correct syntax:


    $result = DB::select('id','name')->from('users')->as_object()->execute();
  • Some of my SQL queries are far too complex for me to want to convert them to query-builder syntax (correlated sub-queries etc etc). For the moment, I've set auto_filter_output to false which allows me to use DB::query($sql)->as_object()->execute().

    Congratulations on v1.4!
  • Nobody is stopping you from using
    $result = DB::query('put your very complicated SQL as-is in here')->as_object()->execute();

    and enjoy all (other) benefits of using the query builder.

Howdy, Stranger!

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

In this Discussion