开发者

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 origi开发者_运维问答nal value HERE

    // do work based on the original value

    // do work based on the new, submitted value

    return parent::save();
}


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.


My approach to a similar problem:

/**
 * Returns Record's original values before saving the new ones
 *
 * @return array
 */
public function getOldValues()
{
    $arr_modified = $this->getModified(true);
    $arr = $this->toArray(false);

    foreach ($arr_modified as $k => $v)
    {
        $arr[$k] = $v;
    }

    return $arr;
}



/**
 * Sample usage of getOldValues
 *
 * @param Doctrine_Connection $conn
 */
public function save(Doctrine_Connection $conn = null)
{
    $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher();

    /* object values before saving */
    $arr_before = $this->getOldValues();

    $event_name = 'myobject.update';
    if (true == $this->isNew())
    {
        $event_name = 'myobject.add';
    }

    parent::save($conn);

    /* object values after saving */
    $arr_after = $this->toArray(true);

    /* Notify about the record changes */
    if ($dispatcher instanceof sfEventDispatcher)
    {
        $dispatcher->notify(new sfEvent($this, $event_name, array('before' => $arr_before, 'after' => $arr_after)));
    }
}


You may to get value like this: $this->_get('field'); (_set('field', value)) ?

Or you may use of doctrine event listeners


You can do that by overriding processForm(). In the actions get it from the cache and you can:

$form->getObject() ;    //the original object
$request->getParameter($form->getName()) ;    // the new object


you can clone and reload the object to get the values before the changes

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

    // get the original value HERE
    $oldObject = $this->getOldObject($this);

    // do work based on the original value

    // do work based on the new, submitted value

    return parent::save();
}

private function getOldObject(BaseObject $object): BaseObject
{
    $old = clone $object;

    $old->reload(true, $this->con);

    return $old;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜