Doktrin dinleyici - bir alan değişti yalnızca çalıştırmak eylem

3 Cevap php

Alan değişti olmadığını nasıl kontrol edebilirim?

I preSave() özel alan değişti yalnızca, denklem bir eylemi tetiklemek istiyorum

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

Soru bu $bodyBefore ve $bodyNow nasıl olduğunu

3 Cevap

Veritabanını yeniden getirme etmeyin! Bu Doktrin 1.2 için, ben alt sürümleri test değil çalışır.

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

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

De, documentation göz atın.

Sorun Doktrini sorgu yaptığınızda nesne üzerine olmasıdır çünkü Travis'in cevabı, neredeyse haklıydı. Dolayısıyla çözüm:

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)
  {
    ...
  }
}

Bu deneyin.

public function preSave($event)
{
   $id = $event->getInvoker()->id;
   $currentRecord = $this->getTable()->find($id);

   if ($currentRecord->body != $event->getInvoker()->body)
   {
      $event->getEnvoker()->body = $this->_htmlify($event->getEnvoker()->body);
   }   
}