예제 #1
0
파일: Poll.php 프로젝트: bariew/sitown
 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()]);
 }
예제 #2
0
 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');
 }
예제 #3
0
 /**
  * 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.');
     }
 }
예제 #4
0
 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));
 }
예제 #5
0
 /**
  * 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;
 }
예제 #6
0
파일: Vote.php 프로젝트: bariew/sitown
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getQuestion()
 {
     return $this->hasOne(Question::className(), ['id' => 'question_id'])->via('answer');
 }
 public function down()
 {
     $this->dropTable(Question::tableName());
 }
예제 #8
0
 /**
  * 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)];
 }
예제 #9
0
 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');
 }