/** * Creates a new Student model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $student = new Student(); $user = new User(); $person = new Person(); if (Yii::$app->request->post()) { $params = Yii::$app->request->post(); $person->load($params); $user->load($params); $user->password_hash = Yii::$app->getSecurity()->generatePasswordHash($params['User']['password_hash']); $student->load($params); if ($person->validate() && $user->validate() && $student->validate()) { $person->save(false); $user->person_id = $person->id; $user->register(); $student->user_id = $user->id; $student->save(); Yii::$app->session->setFlash('success', 'Se envío un correo de confirmación. Por favor verifique su correo electrónico'); return $this->refresh(); } else { Yii::$app->session->setFlash('danger', 'Ocurrió ff un error al guardar. Vuelve a intentar'); return $this->refresh(); } } else { return $this->render('create', ['student' => $student, 'user' => $user, 'person' => $person]); } }
/** * Creates a new Person model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $modelPerson = new Person(); $modelsHouse = [new House()]; $modelsRoom = [[new Room()]]; if ($modelPerson->load(Yii::$app->request->post())) { $modelsHouse = Model::createMultiple(House::classname()); Model::loadMultiple($modelsHouse, Yii::$app->request->post()); // validate person and houses models $valid = $modelPerson->validate(); $valid = Model::validateMultiple($modelsHouse) && $valid; if (isset($_POST['Room'][0][0])) { foreach ($_POST['Room'] as $indexHouse => $rooms) { foreach ($rooms as $indexRoom => $room) { $data['Room'] = $room; $modelRoom = new Room(); $modelRoom->load($data); $modelsRoom[$indexHouse][$indexRoom] = $modelRoom; $valid = $modelRoom->validate(); } } } if ($valid) { $transaction = Yii::$app->db->beginTransaction(); try { if ($flag = $modelPerson->save(false)) { foreach ($modelsHouse as $indexHouse => $modelHouse) { if ($flag === false) { break; } $modelHouse->person_id = $modelPerson->id; if (!($flag = $modelHouse->save(false))) { break; } if (isset($modelsRoom[$indexHouse]) && is_array($modelsRoom[$indexHouse])) { foreach ($modelsRoom[$indexHouse] as $indexRoom => $modelRoom) { $modelRoom->house_id = $modelHouse->id; if (!($flag = $modelRoom->save(false))) { break; } } } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $modelPerson->id]); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('create', ['modelPerson' => $modelPerson, 'modelsHouse' => empty($modelsHouse) ? [new House()] : $modelsHouse, 'modelsRoom' => empty($modelsRoom) ? [[new Room()]] : $modelsRoom]); }
public function actionPerson() { $model = new Person(); if ($model->load(Yii::$app->request->post())) { if ($model->validate()) { // form inputs are valid, do something here return; } } return $this->render('person', ['model' => $model]); }
public function actionAddFamilyMember() { $this->layout = 'frontend'; // $searchModel = new SearchPerson(); $user = new Person(); $user->scenario = 'add-family'; // Set Family ID $user->family_id = Yii::$app->user->identity->id; if ($user->load(Yii::$app->request->post()) && $user->validate()) { $user->active = 1; if ($user->save()) { $user->image = \yii\web\UploadedFile::getInstances($user, 'image'); $user->uploadPhotos(); Yii::$app->session->setFlash('success', 'Profile successfully updated'); return $this->redirect(['/']); } } return $this->render('family-member-form', ['user' => $user]); }
/** * Creates a new Person model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Person(); $jobModel = new Job(); if ($jobModel->load(Yii::$app->request->post()) && $jobModel->validate() && $model->load(Yii::$app->request->post()) && $model->validate()) { $transaction = $model->getDb()->beginTransaction(); try { if (is_numeric($jobModel->name)) { $jobModel = $jobModel->findOne($jobModel->name); } else { $jobModel = new Job(); $jobModel->load(Yii::$app->request->post()); if (!$jobModel->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($jobModel->tableName())), 'msj' => print_r($jobModel->getErrors(), true)]), 500); } } $model->job_id = $jobModel->id; if (!$model->save(false)) { throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->getErrors(), true)]), 500); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'jobModel' => $jobModel]); } }