public function setStartDate($date) { if ($this->startDate) { // todo: can be better $posts = Posts::find()->where(['PostType' => 2])->orderBy('PostID')->all(); foreach ($posts as $post) { $postIds[] = $post->PostID; } $startDatePost = PostDates::find()->where(['PostID' => $postIds, 'PostDateType' => 1])->one(); $startDatePost->PostDate = $date; $startDatePost->save(); $this->startDate = $date; } else { $post = new Posts(); $post->UserID = Yii::$app->user->getId(); $post->PostType = 2; $post->save(); $postDate = new PostDates(); $postDate->PostDateType = 1; $postDate->PostID = $post->PostID; $postDate->PostDate = $date; $postDate->save(); $this->startDate = $date; } }
/** * @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' => []]); } }
/** * Creates a new Posts model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Posts(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->PostID]); } else { return $this->render('create', ['model' => $model]); } }
public function actionAdd($ingredientId) { // less queries here $postType = Posts::TYPE_USER_INGREDIENTS; $post = Posts::find()->where(['and', ['PostType' => $postType], ['UserID' => Yii::$app->user->getId()]])->one(); if (!$post) { $post = new Posts(); $post->PostType = $postType; $post->UserID = Yii::$app->user->getId(); $post->save(); } $ingredient = Tags::findOne($ingredientId); $post->link('tags', $ingredient); }