Example #1
0
 /**
  * Save PollRecord model
  * @param bool $insert
  */
 public function savePoll($insert = true)
 {
     $poll = $insert === true ? new PollRecord() : PollRecord::findOne($this->item_id);
     $poll->attributes = $this->toArray();
     if ($this->end_date) {
         $poll->end_date = Yii::$app->formatter->asDate($this->end_date, 'y-MM-dd');
     }
     $poll->active = is_array($this->boxes) && in_array(self::PROPERTY_ACTIVE, $this->boxes) ? 1 : 0;
     $poll->main = is_array($this->boxes) && in_array(self::PROPERTY_MAIN, $this->boxes) ? 1 : 0;
     if ($this->item_id && $this->isAnswersEditable) {
         PollAnswerRecord::deleteAll(['poll_id' => $this->item_id]);
     }
     $poll->save(false);
     if (is_array($this->answers) && $this->isAnswersEditable) {
         foreach ($this->answers as $answer) {
             $pollAnswer = new PollAnswerRecord();
             $pollAnswer->poll_id = $poll->id;
             $pollAnswer->answer = $answer;
             $pollAnswer->save();
         }
     }
 }