Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM cache problem
  • Hi

    Those days, i really have the feeling to collect each anormal comportement from fuelphp, it's a quite frustrating. And it's to bad, the framework community isn't as large as she deserve to be. Luckily for me, competents and nice guys are always here to help. So :

    At a moment in my code in a model method, for some convenients reasons, i get an object with relating (an array), get the first key, and set it in the relating. Ex :

    $post = // The query for getting a post by id with related

    $post->tags = key($post->tags);

    return $post;

    But i dont save this modified object. Without regards to this fact, when i get this object in some others parts of my code, and in differents pages, the tag relation still contain the tag object instead of and array with it. And inevitabily, it's blow up when i save this object.

    Following the doc, my first reaction was to try the query with " ->from_cache(false) ". I got the following error : Call to undefined method Orm\Query::from_cache() 

    Great. I tried to search some orm cache issue on google but found nothing. So i tried to learn more about fuelphp orm caching system and again, nothing. I suppose people who really know fuelphp does because of reading it, but I'm french so it's way harder for me to understand heavy pieces of fuelphp code.

    Thanks in advance.
  • HarroHarro
    Accepted Answer
    You have this problem because objects are passed by reference (by default), so every ORM object reference for the same record will point to the same object.

    First remark I'd like to make: your doing it wrong. Clearly you have a need for that key value, but you can extract it anywhere, no need to store it in the object. If you have to store it in the object, you should not use a column property. The ORM supports custom properties since September 2012, so why not use:

    $post->tags_key = key($post->tags);

    and your problem is solved?

    Finally, from_cache() was introduced about tree months ago, and released in 1.6. So many it's time to think about an upgrade? Especially in ORM lots of stuff has been fixed and added in 1.6.

  • Hum, yes, my problem is solved. Thanks a lot Harro (again :p)

    And regarding upgrade, i want to do it but i'm not sure the entire project survive so i'll take the time to testing at the end.
  • understandable.

Howdy, Stranger!

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

In this Discussion