Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
->select() and cache ? : I felt into a trap.
  • Hi there,

    I ask :
    M1->query()->select('field')->where(...)->get(); (notice : field)

    then I ask :

    and : M2->M1 shows nothing but id and field ! (yes, the field of previous query)

    Still on 1.7.
  • HarroHarro
    Accepted Answer
    ORM caches results, so this is 'by design'.

    You should not use select() with ORM to avoid this, unless you know what you're doing.
  • I knew ORM caches (and this is really good !)
    I didn't know select() prevents future hydration. Now I know ;-)

    Thanx for reply !
  • HarroHarro
    Accepted Answer
    select() doesn't prevent hydration.

    What happens is that the hydration process detects it already has a Model object in cache for that table and primary key, so it simply links in the cached object instead of hydrating the result and create a new object.

    As you have noticed, this has side-effects when you do partial selects.

    The reason for this caching is that hydration is a very expensive process, and will make the ORM slow, so you would like to avoid it where possible. Also, it ensures at all code uses the same object, so you can't run in the situation where you call delete() on object A, and have a copy of that same record in object B that doesn't know it was deleted.
  • Thanx a lot for precisions !

    So I would think :
    If I need an extra-light check, like : "is a record with value A in column B exists ? give me its id !" - I use DB.

    Maybe a hint in the doc to prevent future headbangs ?

    Thank you for Fuel : it rocks !
  • HarroHarro
    Accepted Answer
    For every DB access you need to think whether you should use ORM or DB calls.

    The current ORM has quite a few limitations or "quirks", the entire hydration/caching bit being one of them. There is a method from_cache(), which allows you to switch off the cache check, but it doesn't work nicely with relations so it's no fix for your problem.

    We're currently redesigning the ORM for Fuel v2, which is the moment to address these issues. The current code is way to complex so implement such architectural changes.

Howdy, Stranger!

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

In this Discussion