1
votes

I take many hours to find a solution to save the foreign key in the mother table. I'm trying to use embed relation with symfony 1.4 (i have ahDoctrineEasyEmbeddedRelationsPlugin too).

When i read the symfony documentation here

http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms

the schema is :

Product:
  columns:
    name:           { type: string(255), notnull: true }
    price:          { type: decimal, notnull: true }
ProductPhoto:
  columns:
    product_id:     { type: integer }
    filename:       { type: string(255) }
    caption:        { type: string(255), notnull: true }
  relations:
    Product:
      alias:        Product
      foreignType:  many
      foreignAlias: Photos
      onDelete:     cascade

the embedRelation looks like:

// lib/form/doctrine/ProductForm.class.php
public function configure()
{
  // ...

  $this->embedRelation('Photos');
}

In my case i can't do otherway, it's the contrary, the product have the relation keys, i have something like that:

Product:
  columns:
    name:           { type: string(255), notnull: true }
    price:          { type: decimal, notnull: true }
    photoid:        { type: integer }
    ownerid:        { type: integer }
  relations:
    Photo:
      local:        photoid
      foreign:      id
      type:         one
    Owner:
      local:        ownerid
      foreign:      id
      type:         one
Photo:
  columns:
    id :            { type: integer }
    filename:       { type: string(255) }
    caption:        { type: string(255), notnull: true }
owner:
  columns:
    id :            { type: integer }
    firstname:      { type: string(255) }
    lastname:        { type: string(255), notnull: true }

and the embedRelation:

// lib/form/doctrine/ProductForm.class.php
public function configure()
{
  // ...
  $this->embedRelation('Photo');
  $this->embedRelation('Owner');
}

And there are not widget related to the product like name or price to update in the form but only informations coming from photo and owner table.

When i save the new form, photo and owner object are save in the table, and ids are created with sequences. The problem is that Product is never update. photoid and ownerid still stay to null. It's like embedded form Photo and Owner are save after the root form.

Is it possible to use embedrelation in that case? If then what is the correct way to save the foreign keys in Product table when the root form is save in the processForm? What's the trick, thx for help.

1

1 Answers

0
votes

In PhotoForm.class.php use:

$this->useFields(array('filename', 'caption'));

And read the documentation