This quick post describes a necessary step in embedding a relation in a Symfony 1.4 form.
If you have followed Symfony’s official documentation and forums regarding embedding relations in a form, you may have already seen an example of product and productPhoto forms in a single form.
Here’s the schema:
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
Now, when you’re creating/editing the productPhoto form, it’s of paramount importance to tell Symfony not to use product_id field in a form, because that would render an select dropbox with a list of currently available products.
The problem becomes apparent when you try to create new product. Because new product does not yet have a product_id, your product photo will be saved with another product that’s currently selected.
The solution is to use line:
$this->useFields(array('filename', 'caption'));
There are no comments jet.