public function actionAjaxAdd() { Yii::$app->response->format = Response::FORMAT_JSON; if (Yii::$app->request->post('productId') && Yii::$app->request->post('question')) { $model = new Consultation(); $model->user_id = Yii::$app->user->id; $model->username = Yii::$app->user->identity->username; $model->product_id = Yii::$app->request->post('productId'); $model->question = Yii::$app->request->post('question'); if ($model->save()) { return ['status' => 1]; } else { return ['status' => -2]; } } return ['status' => -1]; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Consultation::find(); $query->orderBy(['created_at' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query]); if ($this->load($params) && !$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'product_id' => $this->product_id, 'type' => $this->type, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'question', $this->question])->andFilterWhere(['like', 'answer', $this->answer]); return $dataProvider; }
public function actionConsultation($productId) { $this->layout = false; if (Yii::$app->request->isAjax && $productId) { $query = Consultation::find()->where(['product_id' => $productId, 'status' => Status::STATUS_ACTIVE]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => Yii::$app->params['defaultPageSizeProduct']], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); return $this->render('consultation', ['data' => $dataProvider->getModels(), 'pagination' => $dataProvider->pagination]); } }
/** * @return \yii\db\ActiveQuery */ public function getConsultationsPassed() { return $this->hasMany(Consultation::className(), ['product_id' => 'id'])->where(['status' => Status::STATUS_ACTIVE])->orderBy(['created_at' => SORT_DESC]); }
/** * Finds the Consultation model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Consultation the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Consultation::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }