如何在symfony save方法中获取原始值?

| 我正在编写一个symfony 1.4应用程序,并尝试设置在编辑对象时如果特定值发生更改将运行的代码。 我正在尝试在模型类内而不是在视图内执行此操作,因为只要保存此对象,此方法就会适用。 在用户进行任何更改之前,有没有办法访问对象的原始值? 注意: 该对象尚未保存,因此仍然可以(以某种方式)检索原始值。 码:
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();
}
    
已邀请:
如果在保存时不需要进行计算,则覆盖该列的设置器。 只要设置了一个值,就可以基于原始值进行计算,然后根据新值进行计算,最后调用覆盖的父级设置器来实际设置新值。     
您可能会像这样获得价值:
$this->_get(\'field\'); (_set(\'field\', value)) ?
或者您可以使用主义事件监听器     
我对类似问题的处理方式:
/**
 * 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)));
    }
}
    
您可以通过覆盖
processForm()
来做到这一点。在操作中,从缓存中获取它,您可以:
$form->getObject() ;    //the original object
$request->getParameter($form->getName()) ;    // the new object
    

要回复问题请先登录注册