示例#1
0
 /**
  * Add a sfPollUserAnswer object related to current sfPoll object instance
  *
  * @param  int     $user_id     Typically an integer representing a User PK
  * @param  int     $answer_id   sfPollAnswer PK
  * @param  string  $ip_address  IP address (optionnal)
  * @return sfPollUserAnswer
  * @throws PropelException
  **/
 public function addUserAnswer($user_id, $answer_id, $ip_address = NULL)
 {
     if ($this->isNew() or is_null($this->getId())) {
         throw new PropelException('You cannot add a user answer to an unsaved poll');
     }
     $answer = sfPollAnswerPeer::retrieveByPK($answer_id);
     if (!$answer) {
         throw new PropelException('Unable to retrieve answer associated to poll');
     }
     if ($answer->getPollId() !== $this->getId()) {
         throw new PropelException('The answer provided is not associated with current poll');
     }
     # Checks if user has already voted
     $user_answer = sfPollUserAnswerPeer::getOrCreate($this->getId(), $user_id, $ip_address);
     $user_answer->setPollId($this->getId());
     $user_answer->setAnswerId($answer_id);
     $user_answer->setUserId($user_id);
     $user_answer->setIpAddress($ip_address);
     $user_answer->save();
     return $user_answer;
 }