2010-08-12 55 views
0

我有一個產品表與一個字段來存儲價格。 每一次,這個價格都在變化,我需要保持古代的價值。所以我創建了一個名爲History的表來存儲產品ID,價格和更改日期。 我也創造了我的產品類此方法生成一個新的條目:同時保存一個表中的條目和其他關係條目?

public function updateHistory($modified) {  
    if(array_key_exists('price', $modified)) { 
     $history = new History() 
     $history = new History();$ 
     $history->setProductId($this->getId()); 
     $history->setPrice($modified['price']); 
     $history->save() 
    } 
} 

我把這種方法從presave()方法

public function preSave($event) { 
    $modified = $this->getModified(); 
    $this->updateHistory($modified); 
} 

它工作正常的編輯產品,但不因爲它此時沒有id(產品id在歷史表中爲null),並且如果我在postSave方法中使用updateHistory方法,$ modified爲null;

那麼我怎樣才能創建我的第一個條目在歷史表中的新產品?

謝謝大家的回覆和時間幫助我!

(對不起,我的英語)

回答

0

,如果你想輕鬆地跟蹤產品的歷史,你可以用你的schema.yml文件中的「Versionable」的行爲。這樣,你將有一個涉及很少編碼的歷史。

相關問題