public function save($runValidation = true, $attributes = null) { $transaction = Yii::app()->db->beginTransaction(); try { if (!parent::save($runValidation, $attributes)) { throw new CHttpException(500, 'Registration could not be saved.'); } // remove items that has child $serviceIds = array_combine($this->services, $this->services); foreach ($serviceIds as $sid) { $item = OurService::model()->findByPk($sid); if (!$item) { throw new CHttpException(400, 'Request invalid.'); } if ($item->parent_id) { unset($serviceIds[$item->parent_id]); } } // save registered services foreach ($serviceIds as $sid) { $m = new ServiceRegistrationItem(); $m->service_id = $sid; $m->registration_id = $this->id; if (!$m->save()) { throw new CHttpException(500, 'Registration item could not be saved.'); } } $transaction->commit(); } catch (Exception $ex) { $transaction->rollBack(); throw $ex; } return true; }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = ServiceRegistration::model()->findByPk($id); if ($model === null) { Yii::log("The requested page does not exist."); throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }