getTimeUpdated returning unexpected results

hello everyone,

I have been developing a plugin, users can update an object, then the function returns the last time updated to be stored at the user side--the user can then tell if this object has been changed since they did last.

So, why is it, if the user calls a function like this:

$objects=elgg_get_entities(array("type"=>"object", "subtype"=>"sub1"));
foreach($objects as $object){
      if($object->type=="correct"){
         //update object...
         $object->name="amanda";
         return $object->getTimeUpdated();
      }
}

 

What I have found, is getTimeUpdated is not changed to the current update time. Is this a caching issue? how do I fix this?

note: i tried doing a save: ($object->save() ) then returning, this did not work.

 

thanks for your help,

amanda

  • this gives me two different values, why??

    $object->getTimeUpdated();
    get_entity($object->getGUID())->getTimeUpdated();

  • Hey amanda

    Both ways should return the value of time_updated column from the database I think. What are those two different value? How much do they differ?

    What does the getTimeUpdated() return after saving the object if not the current time?

  • this returns the old updated time:
    $object->getTimeUpdated();

    this returns what i want:
    get_entity($object->getGUID())->getTimeUpdated();

     

    this is what I think is happening:
    I read $object, change something, then getTimeUpdated() is the value in the DB when I initially read $object. I know this, because I have tested it.

    When you do a get_entity call, it reads it AGAIN.
    so if I read it, change it, then call get_entity (i.e. read it again) the value is what is should be.

     

     

    I could be wrong, but from what I have experienced so far, this seems to be what is happening.

    Another thought is, time_updated is not updated fast enough. So when I call getTimeUpdated() the new value is not saved yet, and so the old value is returned.

     

    -Amanda

  • The first thing that the $entity->save() does for an existing entity is that it uses cache_entity() to save the entity also to the global $ENTITY_CACHE. The time_updated is however added to the data in the update_entity() function that the save calls. So this is why the time_updated does not get to the cache.

    I wonder if there's some particular reason why the time_created cannot be added to the entity already before calling the cache_entity() and save() functions. That way the new time_updated could be found also from the cache and not only from the database.

  • yes - time_updated is stored to the db at time an entity is ->saved via ->save() => update_entity() and not before -- cache is only refreshed at that time. and so -- to make time_updated refresh in cache would mean some hacked 'direct' update of either the db or hooking into the core's save() function ;-(