/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Ocorrencia(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Ocorrencia'])) { $model->attributes = $_POST['Ocorrencia']; $model->usuario_id = Yii::app()->user->objeto->id; $model->solicitacao = $_POST['solicitacao']; if ($model->save()) { $tiposSelecionados = $_POST['tipo']; } $peso = ''; foreach ($tiposSelecionados as $tipo) { $x = new OcorrenciaTipo(); $x->ocorrencia_id = $model->id; $x->tipo_id = $tipo; $peso += $tipo; $x->save(); } $y = Ocorrencia::model()->findByPk($model->id); $y->prioridade = $peso; $y->save(); $this->redirect(array('view', 'id' => $model->id)); } $tipo = Tipo::model()->findAll(); $this->render('create', array('model' => $model, 'tipo' => $tipo)); }
/** * 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 = OcorrenciaTipo::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }