覆盖Symfony表单的保存功能

时间:2012-09-23 19:27:37

标签: symfony1 symfony-1.4 symfony-forms

我有两个问题,需要一些帮助。

我有一个表,它由第二个表的外键引用:

member_child:
    _attributes: { phpName: MemberChild }
    id: { type: INTEGER, size: '11', primaryKey: true, autoIncrement: true, required: true }
    member_id: { type: INTEGER, size: '11', required: true, foreignTable: member, foreignReference: id }
    child_id: { type: INTEGER, size: '11', required: true, foreignTable: child, foreignReference: id }

和孩子:

child:
    _attributes: { phpName: Child }
    id: { type: INTEGER, size: '11', primaryKey: true, autoIncrement: true, required: true, onDelete: cascade }
    username: { type: VARCHAR, size: '45', required: true, defaultValue: '' }
    display: { type: TINYINT, size: '1', required: true, defaultValue: '1' }
    ...etc

(显然这是推进)

现在,当我想使用表单创建子对象时,我需要做两件事:

  1. 提交时,提交会员ID
  2. 覆盖doSave函数,因此在创建子项时,我还可以创建member_child对象
  3. 我如何解决这些问题?

2 个答案:

答案 0 :(得分:1)

我同意,你可以像pankar一样使用embedForm说。您也可以覆盖表单的保存方法,如下所示:

$this->childForm = new ChildForm();
$this->childMemberForm = new ChildMemberForm();

//binding, checking if form was sent etc.

if ($this->childForm->isValid() && $this->childMemberForm->isValid())
{
  //save method should return saved object
  $childObject = $this->childForm->save(); 
  //therefore this id could be used by next object
  $this->childMemberForm->save(childObject->getId()); 
}

我希望这会对你有所帮助!

答案 1 :(得分:0)

您可以随时使用父表单中的内置Symfony功能sfForm::embedForm来保存孩子,但我还没有找到一种方法来正常使用它。

前一段时间我遇到的一篇文章确实为我提供了解决方案。 Have a look并查看它是否符合您的需求。当然它在Doctrine中,但我想它可以很容易地在Propel中移植