Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How can I use DB::select() result as object in my views?
  • So this is working: DB::select()->from('images')->execute()->as_array(); But I don't like arrays in my views (2 extra letters) I want to use the object. How can I do that? DB::select()->from('images')->as_object()->execute(); This is giving an error.
  • Hi, i've just bumped into this issue myself, and discovered that although i'm calling as_object() before execute() on my query, it's still not returning a mutable object, and is still throwing the 'database results are read only' exception. Any ideas what may cause this? $results = DB::select('*')
    ->from('field_types')
    ->as_object()
    ->execute(); $data = $results; $this->template->title = "Add A Field"; $this->template->content = View::forge('survey/field_add', $data);
  • Well, it does what you've asked, the data is returned as an object. If you want a specific type of object, then you'll have to specify it as parameter of as_object(), for example as_object('stdClass'). Note that this is not supported by the PDO driver at this moment.
  • execute() returns a Database_Result object. You can access it as an array, and iterate over it. See it as a collection of rows. When you do, and you have used as_object(), it will return a row as an instance of stdClass. If you have used as_object('myclass'), it will return a row as an instance of 'myclass'. Otherwise it will return a row as an assoc array.
  • It's working in the model (foreach...) but when I pass it to the view through $data array I get: Fuel\Core\FuelException [ Error ]: Database results are read-only COREPATH/classes/database/result.php @ line 266 261 * @return void
    262 * @throws Exception
    263 */
    264 final public function offsetSet($offset, $value)
    265 {
    266 throw new \FuelException('Database results are read-only');
    267 }
    268
    269 /**
    270 * Implements [ArrayAccess::offsetUnset], throws an error.
    271 * What's wrong?
  • I think I found the reason here: https://github.com/fuel/parser/issues/11 But does that mean using arrays is preferred and more secure in FuelPHP?
  • If you'd said that in your original post ( instead of "this gives an error" ), I would have been able to give you that answer directly. FuelPHP encodes on output, which means every piece of data you pass on the the view must be mutable. If you use the "untreated" result of a query, you're passing a direct database object, which is read-only by default. If you want tp pass objects instead of array's, use "as_object()" as I wrote earlier. It will return the result as an instance of stdClass, which is mutable.
  • Hi I also received same error. My solution is:

    $orders = DB::select()->from('orders')

    ->as_object('Model_Order')

    ->where('status', 'in', array('canceled', 'complete'))

    ->order_by('created_at', 'asc')

    ->execute()

    ->as_array(); // You can add any DB functions like 'limit' before execute()

    View::forge('orders/index', array('orders' => $orders);

Howdy, Stranger!

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

In this Discussion