2010-03-22 101 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()