【问题标题】:Form with embedded relation won't save Symfony 1.4 Doctrine具有嵌入关系的表单不会保存 Symfony 1.4 Doctrine
【发布时间】:2010-12-06 17:30:25
【问题描述】:

我在 Mix 表单中嵌入了 Mutt 表单: MixForm.class.php:

$this->embedRelation('Mutt');
$form = new MuttForm(null, array(
    'mix' =>$this->getObject(),
));
$this->embedForm('Mutt', $form);
$this->widgetSchema['Mutt'] = $form->getWidgetSchema();
$this->widgetSchema['Mutt']['mix_id'] = new sfWidgetFormInputHidden();
$this->validatorSchema['Mutt'] = $form->getValidatorSchema();

我需要为 Mix 表新创建的 id 表单来填充 Mutt 表中的 mix_id 字段。

<?php echo $form->renderHiddenFields();?>
<?php echo $form['name']->renderRow();?>
<?php echo $form['parent1']->renderRow();?>
<?php echo $form['parent2']->renderRow();?>
<?php echo $form['parent3']->renderRow();?>
<?php echo $form['parent4']->renderRow();?>
<?php echo $form['parent5']->renderRow();?>
<?php echo $form['Mutt']['creator']->renderRow();?>
<?php echo $form['Mutt']['email']->renderRow();?>
<?php echo $form['Mutt']['website']->renderRow();?>
<?php echo $form['Mutt']['caption']->renderRow();?>
<?php echo $form['Mutt']['photo']->renderRow();?>
<?php echo $form['Mutt']['copyright']->renderRow();?>
<?php echo $form['Mutt']->renderHiddenFields();?>

这是我在 modules/mix/actions/actions.class.php 中的操作

public function executeEdit(sfWebRequest $request)
   {
    $this->form = new MixForm();
    if($request->isMethod('post')):
        $this->form->bind($request->getParameter('mix'), $request->getFiles($this->form->getName()));
        if($this->form->isValid()):
            $this->form->save();
            $this->redirect('pure/add');
        endif;
    endif;
   }

表单验证工作正常,但不会保存在任一数据库中。 我做错了什么??

【问题讨论】:

    标签: forms doctrine symfony-1.4 symfony-forms


    【解决方案1】:

    您正在为executeEdit 定义一个动作,但processForm 是进行表单验证和保存到数据库的动作。 executeEdit 是在编辑现有作业时显示表单的操作。 见:http://www.symfony-project.org/jobeet/1_4/Doctrine/en/10#chapter_10_sub_the_form_action

    【讨论】:

    • 我结合了这个动作,在我添加另一个表单之前,这很好地保存了表单。我所做的只是添加 $request->getFiles($this->form->getName()) 来绑定,因为嵌入的表单有一个文件上传组件。
    • 好吧,当有文件上传组件时,它不适用于多个表单,因此是时候采用不同的方法了。在symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms 有一个关于正确嵌入表单的深入内容,该内容深入介绍了嵌入 sfForm 或 sfFormDoctrine 对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多