public function actionCreateEvent() { $user = User::findOne(Yii::$app->request->post('user_id')); if (!$user) { return json_encode(['success' => false, 'data' => ['User not found.']]); } $region = Region::findOne(Yii::$app->request->post('region_id')); if (!$region) { return json_encode(['success' => false, 'data' => ['Region not found.']]); } $genre = Genre::findOne(Yii::$app->request->post('genre_id')); if (!$genre) { return json_encode(['success' => false, 'data' => ['Genre not found.']]); } $start_date = new \DateTime(Yii::$app->request->post('start_date')); $end_date = new \DateTime(Yii::$app->request->post('end_date')); $event = new Event(); $event->user_id = $user->id; $event->title = Yii::$app->request->post('title'); $event->location = Yii::$app->request->post('location'); $event->start_date = $start_date->format('Y-m-d H:i:s'); $event->end_date = $end_date->format('Y-m-d H:i:s'); $event->url = Yii::$app->request->post('url'); $event->notes = Yii::$app->request->post('notes'); $event->image = Yii::$app->request->post('image'); $event->visibility = Yii::$app->request->post('visibility'); $event->region_id = $region->id; $event->genre_id = $genre->id; if ($event->save()) { if ($event->visibility == Event::VISIBILITY_PUBLIC) { $eventPost = new EventPost(); $eventPost->event_id = $event->id; $eventPost->status = EventPost::STATUS_NEW; $eventPost->save(); } return json_encode(['success' => true, 'data' => '']); } else { return json_encode(['success' => false, 'data' => $event->getErrors()]); } }
/** * Finds the Genre model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Genre the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Genre::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }