Alex Rivera | Logout

Doctrine listener - run action only if a field has changed

Asked 2010-01-13T19:01:55.827
15

How do I check if field has changed?

I'd like to trigger an action in preSave() only if specific field has changed, e.q.

public function preSave() {
    if ($bodyBefore != $bodyNow) {
         $this->html = $this->_htmlify($bodyNow);
    }
} 

The question is how to get this $bodyBefore and $bodyNow

Edit
Report

1 Answer

2

Travis's answer was almost right, because the problem is that the object is overwritten when you do the Doctrine query. So the solution is:

public function preSave($event)
{
  // Change the attribute to not overwrite the object
  $oDoctrineManager = Doctrine_Manager::getInstance(); 
  $oDoctrineManager->setAttribute(Doctrine::ATTR_HYDRATE_OVERWRITE, false); 

  $newRecord = $event->getInvoker();
  $oldRecord = $this->getTable()->find($id);

  if ($oldRecord['title'] != $newRecord->title)
  {
    ...
  }
}
answered 2010-02-18T22:40:43.947

Your Answer