Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
UpdatedAt Observer for custom fields?!?
  • Hi there, I'm new to FuelPHP and I'm impressed how powerful it is. Thanks to everyone involved!!
    My question is, can I use the UpdatedAt observer in a way that is only gonna be called when a specific field is updated??
    I explain, for example I have a table on mysql called 'currencies' (which has 'id', 'currency', 'exchange_rate' and 'exchange_rate_updated_at' fields). What I want to do is, everytime that 'exchange_rate' field is updated, to save UNIX timestamp on 'exchange_rate_updated_at' with the date when it has been updated.
    I'm not sure if I can use the UpdatedAt observer for this purpose (apparently this observer is gonna be called everytime a field is updated, and I want it to just be called when 'exchange_rate' is updated) or I have to write my own one. If so, please give me some examples or instructions because I don't have a clue how I could create an observer like this. Thanks for your time! and sorry for my english by the way..
    Cheers!
  • Welcome here j-one. No problem with your english. You are correct, the UpdatedAt observer will update the timestamp whenever something is changed in the object. Writing your own observer class, or using the ObserverSelf in combination with a method in the model itself, is the way to go. If you go for the observer class, know that the original data is not accessible from outside the object, so you might have to add a method is_rate_changed() to the model that will do the comparison.
  • Thanks for replying!
    It looks that the easiest way is to use ObserverSelf, rather than creating another observer..
    But I'm still a bit lost and can't find any example on the documentation..
    I think that I have to call the ObserverSelf on the model and create a function called somethink like '_event_before_insert()' on the same model and with this method do the comparison, correct me if I'm wrong..
    The thing is how can I compare the old value with the new one, or somehow check that this field has been updated? (Can't find any help on the forum or documentation..) Thanks a lot!
  • You add:
    protected static $_observers = array(
        'Orm\\Observer_Self' => array(
            'events' => array('before_insert'),
        ),
    );
    
    to define the observer. You then define a public method _event_before_insert() (in this example). It will then be called before all insert operations. Note that it's not a static method, it's called on the model instance, so it has access to all model object internals via $this. To do something special in there it's impossible to document, so you'll have to dive into the internals of the model class. If you check the is_changed() method, you'll see that it checks the object property against the value of that property in $this->_original.
  • Thanks again
    But I'm still not able to sort it out!!
    I define the observer just like you said, and then set a function like this public function _event_before_insert()
    {
    if($this->is_changed('exchange_rate'))
    {
    $this->exchange_rate_updated_at = 'timestamp';
    $this->save();
    }
    } But it's not doing anything! I guess it's because I don't know how to use the is_changed() function or I'm setting the wrong event (maybe 'after_insert' or another one), or even because I'm still setting the Observer in a wrong way.
    Anyway, thanks a lot for your help!

Howdy, Stranger!

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

In this Discussion