Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine listener - run action only if a field has changed

Tags:

php

doctrine

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

like image 685
takeshin Avatar asked Jan 13 '10 19:01

takeshin


1 Answers

Please don't fetch the database again! This works for Doctrine 1.2, I haven't tested lower versions.

// in your model class
public function preSave($event) {
  if (!$this->isModified())
    return;

  $modifiedFields = $this->getModified();
  if (array_key_exists('title', $modifiedFields)) {
    // your code
  }
}

Check out the documentation, too.

like image 121
bartman Avatar answered Sep 21 '22 21:09

bartman