2010-01-13 5 views

Respuesta

23

¡No vuelva a buscar la base de datos! Esto funciona para Doctrine 1.2, no he probado versiones inferiores.

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

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

Mira la documentation, también.

-1

probar esto.

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

Cuando agrego el parámetro '$ event' a' preSave() 'el método no se ejecuta en absoluto. – takeshin

+0

¿Qué versión de Doctrine estás usando? – Travis

+0

Uso Doctrine 1.2.1 – takeshin

3

La respuesta de Travis fue casi correcta, porque el problema es que el objeto se sobrescribe cuando se realiza la consulta de Doctrine. Entonces la solución es:

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