public function setUpDown($id, $attitude) { $model = $this->findModel($id); if (!in_array(Yii::$app->user->id, explode(",", $model->part))) { $model->{$attitude} = $model->{$attitude} + 1; $model->part = $model->part . "," . Yii::$app->user->id; if ($model->save()) { $suggesDetail = new SuggesDetail(); $suggesDetail->sugg_id = $id; $suggesDetail->part_id = Yii::$app->user->id; $suggesDetail->attitude = $attitude === "up" ? 1 : 0; if ($suggesDetail->save()) { Yii::$app->session->setFlash('success', '提交成功'); return $this->redirect(['view', 'id' => $model->id]); } else { Yii::$app->session->setFlash('error', '提交失败'); return $this->redirect(['view', 'id' => $model->id]); } } } else { Yii::$app->session->setFlash('error', '抱歉,您已经提交同意或反对了'); return $this->redirect(['view', 'id' => $model->id]); } }
/** * Displays a single Suggestion model. * @param integer $id * @return mixed */ public function actionView($id) { $suggesDetailProvider = new ActiveDataProvider(['query' => SuggesDetail::find()->where(['sugg_id' => $id]), 'pagination' => ['pageSize' => 20]]); return $this->render('view', ['model' => $this->findModel($id), 'suggesDetailProvider' => $suggesDetailProvider]); }