コード例 #1
0
 protected function doSave($con)
 {
     if (null === $con) {
         $con = $this->getConnection();
     }
     $pollid = $this->getValue('poll_id');
     // creating a new answer
     $answer = new aPollAnswer();
     $answer->setPollId($pollid);
     $answer->setRemoteAddress($this->getValue('remote_address'));
     $answer->setCulture($this->getValue('culture'));
     $answer->setIsNew(true);
     $answer->save();
     $aid = $answer->getId();
     // recovering all fields that must be saved in the DB
     $answer_fields = new Doctrine_Collection('aPollAnswerField');
     $fields_to_save = $this->getFieldsToSave();
     if (null === $fields_to_save) {
         throw new sfException('To save this form, you must define which fields must be saved using setFieldsToSave()');
     }
     // for each field, we create a new aPollAnswerField, which is linked to a aPollAnswer
     // (and obviously to the poll)
     foreach ($fields_to_save as $field) {
         $v = $this->getValue($field);
         if (is_null($v) || '' === $v) {
             continue;
         }
         $af = new aPollAnswerField();
         $af->setPollId($pollid);
         $af->setAnswerId($aid);
         $af->setName($field);
         if (is_array($v)) {
             $v = serialize($v);
         }
         $af->setValue($v);
         $answer_fields->add($af);
     }
     try {
         // Once all fields set, we save all of them
         if (count($answer_fields)) {
             $answer_fields->save();
         }
     } catch (Exception $e) {
         $answer->delete();
         throw $e;
     }
     return $answer;
 }