My situation is like this, I have a master detail relation on Sales and SalesDetail. Where Sales store info about date, customer name and so on and SalesDetail store info about quantity, product and unitPrice + discount, also isPrintable field to distinguish that this line will be printed or not (it is there for simplify the stock calculation).
The product can be virtual or physical. If physical then we just add that detail with isPrintable = 1 but if it is virtual, I add the virtual product with isPrintable = 1, also all the members ( I have MemberProduct table to store if the product is virtual then, it consists of which physical product) with the isPrintable = 0.
Create new detail is ok since I can just put a database transaction in one transaction to save the the virtual product and physical products member.
On delete is also easy since I can just deleteAll
But, I have a problem in update. I tried to use afterSave() but always do nothing (seems error in the save() line_
A SalesDetail object can have either virtual or physical product. Whenever I save a virtual product, I want to update some value of the correspond physical product. My solution to this is to use an afterSave() method. However, the code failed to update any record:
protected function afterSave() {
if ($this->product->type == Product::VIRTUAL) {
$criteria = new CDbCriteria;
$criteria->condition = 'salesFk = :salesFk AND parentProduct = :pProd';
$criteria->params = array(':salesFk' => $this->salesFk, ':pProd' => $this->productFk);
$children = SalesDetail::model()->findAll($criteria);
$memo = "";
foreach ($children as $child) {
$memo .= $child->id . " ";
}
$this->memoLine = $memo;
$this->save(); // <- not save the row, it gave an error message
}
parent::afterSave();
}