我有一個產品表,其中每種類型的關聯(另一個表)都有一個價格。保存一個相關模型的多個實例(has_many)
這使我的表:
# product
# associate_types
## description
# associate_prices
## product_id
## associate_type_id
## price
產品的型號關係:
'prices' => array(self::HAS_MANY, 'AssociatePrices', 'id_product')
AssociatePrices:
'associateType' => array(self::BELONGS_TO, 'AssociateTypes', 'id_associate_type'),
現在,我展示產品形態各種AssociateTypes以便用戶填寫所有AssociateType的價格,我管理得非常好:我正在獲取associateTypes,這個給定產品已經插入的價格,然後交叉它們。
爲了更新/創建的條目,我對產品的afterSave()下面的代碼:
if(isset($_POST['AssociatePrices'])) {
$this->pricesDup = array(); // overwriting current price setting
foreach ($_POST['AssociatePrices'] as $i => $price) {
$this->pricesDup[$i] = AssociatePrices::model();
$this->pricesDup[$i]->attributes = $price;
$this->pricesDup[$i]->id_product = $this->id;
$this->pricesDup[$i]->save(false);
}
}
$this->pricesDup
是我剛做出來,因爲我不能$this->prices
發揮性能。
什麼是happing,即使save()方法返回true,數據庫保持不變。
我已經從yii框架論壇上閱讀了很多東西,但沒有任何東西實際應用於我的案例。我究竟做錯了什麼?
非常感謝
編輯:產品型號也有beforeSave()函數,但它不是這個問題有關。即使我發佈(的一部分)在這裏根據要求。
/**
* This function's mission is to handle the file upload.
*/
public function beforeSave()
{
parent::beforeSave();
// File uploading
if (is_object($this->image)) {
$dir = PRODUCTS_FILES_PATH;
$current = Yii::app()->getRequest()->getPost('currentImage');
if (!empty($current))
@unlink($dir.$current);
$name = $this->image->name;
$j = 1;
while (file_exists($dir . $name)) {
//....
}
$this->image->saveAs($dir.$name, true);
$this->image = $name;
}
return true;
}
沒有。 $ this-> pricesDup [$ i] - > attributes = $ price; $ price來自$ _POST vars :) –
那麼,哪裏是新的模型實例。贊:檢查更新回答 你在該控制器中有'beforeSave()'函數嗎?如果是的話,那麼也請告訴我們。 –
有一個'beforeSave',但它與這個問題(價格)沒有關係。即使我會發布它。 「創建一個新的模型實例,以便它可以保存在數據庫中」我在foreach中這樣做。 –