hi friends, im beginner in fuelphp, i need a help, i have a controller to get data from my model,
here's my controller:
use \Model\Mcompany; class Controller_main extends Controller { function action_index() { $data = View::forge('layout/index'); $data->name=Mcompany::get_name(); return Response::forge($data); } }
n my model:
namespace Model; use DB; class Mcompany extends \Model { public static function get_name() { $que=DB::query("SELECT*FROM company"); $result=$que->execute(); } }
when i print_r() the $result, it display all data in company table. after that, i try to write
return $result;
so that the result can be fetched to my controller to display to my view file, but error show up,
Fuel\Core\FuelException [ Error ]: Database results are read-only
i must've made a big mistake, so, can you guys help me? thanks lot b4.
By default it is an object. So you don't need to cast it as an object.
I found that it's not the problem if you return the result.
Did you try to modify the result? The "Database results are read-only" error will raise when you want to modify the result object. Convert to array then will allow you to modify it.
P.S. I'm not from FuelPHP. I'm just one of the user.
By default, DB returns database driver result objects, as it's the fastest way to produce results. In case of the MySQL driver for example, it will return MySQL_Result_Object. These objects are by design read-only.
This is not a problem, until you pass an object like this to a view, and you don't disable output encoding. When rendering the view the Security class will html encode all data passed to the view, which is not possible with objects of this type, hence the error message. CI doesn't have this issue, because it doesn't provide any security at this point.
So:
Either pass objects of this type to a view using set_safe(), or use as_object('stdClass') on the query to produce standard objects instead of driver result objects. stdClass objects can be encoded without problems.