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?
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.