public function afterSave() { // Process related products if ($this->_related !== null) { $this->clearRelatedProducts(); foreach ($this->_related as $id) { $related = new StoreRelatedProduct(); $related->product_id = $this->id; $related->related_id = $id; $related->save(); } } // Save configurable attributes if ($this->_configurable_attribute_changed === true) { // Clear Yii::app()->db->createCommand()->delete('StoreProductConfigurableAttributes', 'product_id = :id', array(':id' => $this->id)); foreach ($this->_configurable_attributes as $attr_id) { Yii::app()->db->createCommand()->insert('StoreProductConfigurableAttributes', array('product_id' => $this->id, 'attribute_id' => $attr_id)); } } // Process min and max price for configurable product if ($this->use_configurations) { $this->updatePrices($this); } else { // Check if product is configuration $query = Yii::app()->db->createCommand()->from('StoreProductConfigurations t')->where(array('in', 't.configurable_id', array($this->id)))->queryAll(); foreach ($query as $row) { $model = StoreProduct::model()->findByPk($row['product_id']); if ($model) { $this->updatePrices($model); } } } $this->updated = date('Y-m-d H:i:s'); return parent::afterSave(); }
/** * Copy related products * * @param StoreProduct $original * @param StoreProduct $copy */ protected function copyRelated(StoreProduct $original, StoreProduct $copy) { $related = $original->related; if (!empty($related)) { foreach ($related as $p) { $model = new StoreRelatedProduct(); $model->product_id = $copy->id; $model->related_id = $p->related_id; $model->save(); } } }