/** * Creates a new Work model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $workModel = new Work(); $metaModel = new MetaTag(); $image1Model = new Image(); $image1Model->setScenario('add'); $image2Model = new Image(); $transaction = null; try { if (!$workModel->load(Yii::$app->request->post())) { throw new Exception(); } $transaction = \yii::$app->getDb()->beginTransaction(); $image1Storage = new ImageStorage($image1Model); $image2Storage = new ImageStorage($image2Model); if ($image1Storage->isSendFile('image1') && $image1Storage->save()) { $image1Model->save(); $workModel->image1 = $image1Model->id; } if ($image2Storage->isSendFile('image2') && $image2Storage->save()) { $image2Model->save(); $workModel->image2 = $image2Model->id; } if (!$workModel->save()) { throw new Exception(); } $metaModel->load(Yii::$app->request->post()); $metaModel->link = '/works/details/' . $workModel->id; if (!$metaModel->save()) { throw new Exception(); } $transaction->commit(); return $this->redirect(['index']); } catch (Exception $e) { if (isset($image1Storage)) { $image1Storage->delete(); } if (isset($image2Storage)) { $image2Storage->delete(); } if ($transaction instanceof Transaction) { $transaction->rollBack(); } } return $this->render('create', ['workModel' => $workModel, 'metaModel' => $metaModel, 'image1Model' => $image1Model, 'image2Model' => $image2Model]); }
public function actionCreate2() { $formatter = Yii::$app->formatter; $work = new Work(); $timesheet = new Timesheet(); $isUpdated = false; $isCreated = false; if (Yii::$app->request->post()) { $formAttributes = Yii::$app->request->post('createForm'); $date = $formAttributes['date']; $availTimesheet = Timesheet::find()->where(['date' => $date, 'user_id' => Yii::$app->user->identity->id])->one(); if ($availTimesheet) { // timesheet is available if ($availTimesheet->status) { Yii::$app->session->setFlash('CreateTimesheetFailed'); return $this->render('create'); } $work->timesheet_id = $availTimesheet->id; $availTimesheet->updated_at = time(); if ($availTimesheet->update()) { $isUpdated = true; } } else { $timesheet->created_at = time(); $timesheet->updated_at = time(); $timesheet->date = $date; $timesheet->user_id = Yii::$app->user->identity->id; $timesheet->point = 0; $timesheet->director_comment = ''; $timesheet->status = 0; if ($timesheet->save()) { $isCreated = true; } } if ($isUpdated || $isCreated && ($work->timesheet_id = $timesheet->id)) { $process = Process::find()->where(['process_name' => $formAttributes['process_name']])->one(); $team = Team::find()->where(['team_name' => $formAttributes['team_name']])->one(); $work->process_id = $process->id; $work->team_id = $team->id; $work->work_time = $formAttributes['work_time']; $work->work_name = $formAttributes['work_name']; $work->comment = $formAttributes['comment']; $work->created_at = time(); $work->updated_at = time(); if ($work->save()) { return $this->goBack(); } else { return $this->render('create'); } } } return $this->render('create'); }