/** * Deletes an existing Event model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $eventPost = EventPost::find()->where('event_id = :id', [':id' => $id])->one(); if ($eventPost) { $eventPost->delete(); } $this->findModel($id)->delete(); return $this->redirect(['index']); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = EventPost::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'event_id' => $this->event_id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'message', $this->message]); return $dataProvider; }
/** * Finds the EventPost model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return EventPost the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = EventPost::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
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()]); } }
/** * @return \yii\db\ActiveQuery */ public function getEventPosts() { return $this->hasMany(EventPost::className(), ['event_id' => 'id']); }