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;
那麼我怎樣才能創建我的第一個條目在歷史表中的新產品?
謝謝大家的回覆和時間幫助我!
(對不起,我的英語)