Symfony 1.4 forms doesn't save foreign key with embedRelation

776 Views Asked by At

Here is the situation:

I have a Model A and Model B. Model A has a foreign key (modelb_id) for Model B.

Both models have their form. I'm embedding the form for Model B into the form A.

class ModelAForm extends ....{

  public function configure(){
    unset($this['modelb_id']);
    $this->embedRelation('ModelB');
  }
}

The problem:

When I save the form, it does save the ModelB, but it doesn't change the value of the foreign key in ModelA (in model A, the modelb_id is still empty).

Anyone has a clue?

2

There are 2 best solutions below

0
On

I always had problems with embedRelation() until I found a plugin named ahDoctrineEasyEmbeddedRelationsPlugin which solved ALL of my embedding issues with forms.

I believe that it is better to use this plugin than to debug your embeds over and over. :)

0
On

If you want to update related objects on Save of one form. You can override the BasesfForm::doUpdateObject method.

In your Form.class.php, add something like:

 /* If you want to add some logic before updating or update other associated
  * objects, this is the method to override.
  *
  * @param array $values An array of values
  */
  public function doUpdateObject($values) {
    // Handle the normal stuff that this method does
      $this->getObject()->fromArray($values, BasePeer::TYPE_FIELDNAME);
      $obj = $this->getObject();

    // and, get the needed value
      $val = $obj->getDesiredPropertyValue();

    // use the value to update the related thing
      foreach ($obj->getRelatedObjects() as $related)
            {
              $related->setColumnNamedThing($val);
            }
   }