Doctrine symfony setDefault不保存值

Doctrine symfony setDefault不保存值,doctrine,symfony-1.4,setdefault,Doctrine,Symfony 1.4,Setdefault,我在操作中定义一个表单,并将其传递给模板。保存时无法设置默认值。你能告诉我我做错了什么吗 在模块\问题\操作中: public function executeNew(sfWebRequest $request) { $this->form = new questionForm(); if ($this->getRequest()->getMethod() == sfRequest::POST) { // create questi

我在操作中定义一个表单,并将其传递给模板。保存时无法设置默认值。你能告诉我我做错了什么吗

在模块\问题\操作中:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new questionForm();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      // create question
      $user = $this->getUser()->getGuardUser()->getId();

      $question = new Question();
      $question->setQuestionTitle($this->getRequestParameter('question_title'));
      $question->setQuestionBody($this->getRequestParameter('question_body'));
      $question->setUserId($user);
      $question->save();

      $question->addTagsForUser($this->getRequestParameter('tag'), $user);

      return $this->redirect('question/index');
    }

    //set current user as default
    $this->form->setDefault('user_id',$this->getUser()->getGuardUser()->getId());
  }
在\modules\question\templates中:

<?php echo $form->renderFormTag('question/new') ?>
  <table>
    <?php echo $form['question_title']->renderRow(array('size' => 40), 'Question title:') ?>
    <?php echo $form['question_body']->renderRow(array('cols' => 40, 'rows' => 10), 'Description:') ?>

    <?php echo input_auto_complete_tag('tag', '', '@tag_autocomplete', 'autocomplete=off', 'use_style=true') ?>
    <tr>
      <td colspan="2">
        <input type="submit" value="ask it" />
      </td>
    </tr>
  </table>
</form>

两个选项之一:

  • 将setDefault代码放入QuestionForm类中
  • 将setDefault代码放在实例化表单对象之后
  • 常见的做法是选项#1,因为它会导致表单配置和操作之间的松散耦合