Doctrine Date in Override Save Mode / Before Save

I have a Doctrine model with the date field “date_of_birth” (symfony form date) that is filled by the user, everything works 100%, it saves db as expected, however, in the save () method of the model I need to get the value of this field before saving. My problem is that when I try to get the date value, it returns an empty string if its a new record and old value if it is an existing record

public function save(Doctrine_Connection $conn = null) { $dob = $this->getDateOfBirth(); // returns empty str if new and old value if existing $dob = $this->date_of_birth; //also returns empty str return parent::save($conn); } 

How can I get the value of this field, data is saved

+6
save symfony1 doctrine
source share
2 answers

In Doctrine 1.2, you can override preSave pseudo-event:

 // In your model class public function preSave($event) { $dob = $this->getDateOfBirth(); //do whatever you need parent::preSave($event); } 

In Doctrine 2.1, function names are changed.

+7
source share

The common pseudo-events in the doctrine use “new” values, however there is a getModified () method, and it does exactly what you need.

 $modifiedFields = $this->getModified(true); if(isset($modifiedFields['date_of_birth'])) { //index is available only after change echo $modifiedFields['date_of_birth']; //old value } 

more info from doc about getModified ()

+2
source share

All Articles