public function run() { $vote = $this->question->getUserVote() ?: new Vote(); if ($this->question->isOpen() && $vote->isNewRecord && $vote->load(\Yii::$app->request->post()) && $vote->save()) { Yii::$app->controller->refresh(); Yii::$app->end(); } $answerQuery = $this->question->getAnswers()->joinWith('votes')->addSelect(['*', 'voteCount' => 'count(user_id)'])->groupBy('id'); return $this->render('poll', ['model' => $vote, 'question' => $this->question, 'answers' => $answerQuery->all()]); }
public function up() { $table = \app\modules\poll\models\Comment::tableName(); $this->createTable($table, ['id' => $this->primaryKey(), 'user_id' => $this->integer(), 'question_id' => $this->integer(), 'created_at' => $this->integer(), 'message' => $this->text()]); MigrationHelper::addForeignKey($table, 'user_id', \app\modules\user\models\User::tableName(), 'id', 'SET NULL', 'CASCADE'); MigrationHelper::addForeignKey($table, 'question_id', \app\modules\poll\models\Question::tableName(), 'id', 'CASCADE', 'CASCADE'); }
/** * Finds the Question model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Question the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Question::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionTmp() { $model = Question::find()->one(); $model->attributes = ['title' => 123]; $diff = new \cogpowered\FineDiff\Diff(); if (!isset($model->dirtyAttributes)) { $old = []; $new = $model->attributes; } else { $old = array_intersect_key($model->oldAttributes, $model->dirtyAttributes); $new = array_intersect_key($model->attributes, $model->dirtyAttributes); } echo $diff->render(json_encode($old), json_encode($new)); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Question::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'type' => $this->type, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getQuestion() { return $this->hasOne(Question::className(), ['id' => 'question_id'])->via('answer'); }
public function down() { $this->dropTable(Question::tableName()); }
/** * Gets the last poll related to this pull request * @return Question */ public function getPoll() { if (static::$_polls === null) { static::$_polls = Question::find()->where(['like', 'relation_id', get_class($this)])->indexBy('relation_id')->all(); } return @static::$_polls[ModelHelper::getRelationId($this)]; }
public function up() { $table = Answer::tableName(); $this->createTable($table, ['id' => $this->primaryKey(), 'question_id' => $this->integer(), 'title' => $this->string(), 'value' => $this->string()]); MigrationHelper::addForeignKey($table, 'question_id', Question::tableName(), 'id', 'CASCADE', 'CASCADE'); }