Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
newbie, how to return DB result from model to controller
  • 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.
  • I think you want to return the result in array. you can use as_array() method and return it.
    Example from the documentation.

    $result = DB::select()->from('users')->execute();
    foreach($result as $item)
    {
    // do something with $item
    }

    $result_array = $result->as_array();
    foreach($result_array as $item)
    {
    // do something with $item
    }

  • thanks so much ssiangge, it works,

    but, how can i return the result as object, not array.
    i write this

    $que=DB::query("SELECT*FROM company")->execute();
    $result=$que->as_object();
    return $result;

    but error:

    ErrorException [ Error ]: Call to undefined method Fuel\Core\Database_Result_Cached::as_object()

    i do so, bcoz i wanna do just like in CI:

    foreach($myComp as $dt)
    {
        echo $dt->name."<br>";
    }


    so, how should i do ? thanks 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.

Howdy, Stranger!

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

In this Discussion