Search code examples
phpsymfony1symfony-1.4overloading

how to get the original value inside the symfony save method?


I am writing a symfony 1.4 app and am attempting to set up code that will run if a specific value changes when an object is edited.

I am trying to do this within the model class rather than inside the view since this will apply whenever this object is saved.

Is there a way to access the original value of the object before whatever changes were made by the user?

Note:

The object has not been saved yet, so it will still be possible (somehow) to retrieve the original value.

CODE:

public function save()
{
    if($this->isNew())
        $this->getAcctRelatedByAccountId()->updateCurrentBalance(($this->isAdditive()) ? $this->getAmount(): $this->getAmount()*-1);

    // get the original value HERE

    // do work based on the original value

    // do work based on the new, submitted value

    return parent::save();
}

Solution

  • If you do not need the calculations be done when saving, overwrite the setter of the column.

    Whenever a value is set, you can do your calculations based on the original value, then on the new one, finally call the overwritten parent setter to actually set the new value.