Search code examples
symfony1symfony-1.4symfony-forms

Overriding Symfony Form's Save Function


I have two issues, need some help with.

I have a table which is referenced by a foreign key to a second table:

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 }

and child:

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

(obviously this is propel)

Now, when I want to create a child object, using a form, I need to do two things:

  1. On submit, submit a member id
  2. override the doSave function so when the child is created, I can also create the member_child object

How can I accomplish these issues?


Solution

  • I agree, you can use embedForm like pankar said. Also you can override save method of your forms like this:

    $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()); 
    }
    

    I hope that will help you!