Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How can I renew data in the existing ORM\Model object?
  • Let me have some loaded ORM\Model object, for example, $model = Model_Something:find($id);
    While I was doing some work, data in database changed (I am sure).
    How can I renew data in the existing object $model without reassigning var with command above?

    Do we have something like $model->reload() or $model->renew()?
  • Harro VertonHarro Verton
    Accepted Answer
    No, if the data changes outside of the ORM, the only way to refresh the data is to rerun the query.

    Why is rerunning the query a problem? Are you mis-using the ORM model for other things, as it should only contain the DB data, so a reload can't be a problem?

    p.s. the ORM doesn't return variables, it returns object references to the ORM cache, if you rerun a query, even when using a different variable to store the result, the cache is updated and every variable that references the cache is updated to.

    If you do

    $a = Model_Something::find(1);
    $b = Model_Something::find(1);
    var_dump($a, $b);

    you will see that $a and $b reference the same object.

    The ORM does this to make sure data remains consistent within the application. 

    Likewise, if you would do

    $a->field = "new value";

    you would see the change.
  • Thank you, Harro. Can you tell me how can I get new object (not from cache), when I run the same query?
  • Harro VertonHarro Verton
    Accepted Answer
    The cleanest way, without having to fiddle with the ORM, is to use a normal DB query:

    $a = DB::select()->from('table')->where('id, '=', $id)->as_object('Model_Something')->execute();

Howdy, Stranger!

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

In this Discussion