/** * @todo: remove creates from here * @todo: make it readable, use Vacations/Dates */ public function actionCreate() { $model = new Posts(); if ($model->load(Yii::$app->request->post())) { $model->UserID = \Yii::$app->user->getId(); $model->PostType = Posts::TYPE_VACATIONS; if ($model->save()) { // todo: check the data! $hd = explode(",", Yii::$app->request->post()['holidaydates']); if (count($hd) == 2) { PostDates::deleteAll(['PostID' => $model->PostID]); $begin = new \DateTime($hd[0]); $end = new \DateTime($hd[1]); $end = $end->modify('+1 day'); $interval = new \DateInterval('P1D'); $daterange = new \DatePeriod($begin, $interval, $end); foreach ($daterange as $date) { $d = new PostDates(); $d->PostID = $model->PostID; $d->PostDate = $date->format("Y-m-d"); $d->PostDateType = 0; $d->save(); } } return $this->redirect(['vacations/index', 'username' => Yii::$app->user->identity->username]); } } else { // todo: ??? return $this->render('create', ['model' => $model, 'postdates' => []]); } }
/** * Delete Post with dates. * * @todo: use beforeDelete for global delete all Post relations */ public function deletePastDates($postId) { PostDates::deleteAll(['PostID' => $postId]); Posts::deleteAll(['PostID' => $postId]); }
/** * @return \yii\db\ActiveQuery */ public function getPostDates() { return $this->hasMany(PostDates::className(), ['PostID' => 'PostID']); }