Doctrine listener - 仅在字段更改时运行操作

2022-01-16 00:00:00 php doctrine

如何检查字段是否已更改?

How do I check if field has changed?

我想在 preSave() 中仅在特定字段发生更改时触发操作,例如

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);
    }
} 

问题是如何得到这个$bodyBefore$bodyNow

推荐答案

请不要再取数据库了!这适用于 Doctrine 1.2,我还没有测试过低版本.

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
  }
}

也请查看文档.

相关文章