2010-03-22 42 views
6

我有一个日期字段“date_of_birth”(symfony表单日期)的Doctrine模型,由用户填写所有作品100%它保存到数据库的预期,但在模型中save()方法我需要在保存发生之前检索此字段的值。我的问题是,当试图获取日期值返回如果一个新的记录,如果它是一个现有的记录保存覆盖/保存之前的学说日期

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

我怎样才能检索字段的值旧值beore数据被保存空字符串

回答

7

教义1.2,您可以覆盖preSave伪事件:

// In your model class 
public function preSave($event) { 
    $dob = $this->getDateOfBirth(); 

    //do whatever you need 

    parent::preSave($event); 
} 

In Doctrine 2.1 the function names changed.

+0

链接的破碎...:/ – Carlos 2016-11-28 11:26:15

+0

好链路上的固定电流文档 – Benoit 2016-12-12 14:49:49

+0

卡洛斯也没有必要downvote我,因为教义项目没有这些年来保持自己的链接(6现在半年) – Benoit 2016-12-12 15:01:11

2

Generaly伪事件我n教义使用“新”值,但有getModified()方法,它正是你所需要的。

$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()