Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Collection Form does not fill join column

I have 2 entities, Product and Product_Types.

This is the relationship between them:

manyToOne:
    product:
      targetEntity: Product
      joinColumn:
        name: product_id
        referencedColumnName: id

When I add a new Product, I want to be able to add types to this product with the same form.

            ->add('productColor', 'collection', array("type" => new ProductColorType(), "allow_add" => true, 'prototype' => true))

This is how I embed the form(s).

The 'data_class' is set for both ProductType and ProductTypesType, and when I add a new product with types, everything is fine, except for one thing, symfony/doctrine does not set the 'product_id' column, so there will be no relationship between my Product and its types.

like image 820
Tom Avatar asked Jan 05 '12 19:01

Tom


1 Answers

You need to cross link your entities manually. Neither Symfony nor Doctrine do this for you. You can do this for example within an add...() method:

public function getProductColors()
{
    return $this->productColors;
}

public function addProductColor(ProductColor $color)
{
    $this->productColors->add($color);
    $color->setProduct($this);
}

Then three requirements have to be fulfilled in order for this to work:

  • you are running Symfony master
  • you are running Doctrine 2.1.7/2.2.3
  • you set the "by_reference" option of the "collection" field to false

If you do all this, you should be fine.

like image 104
Bernhard Schussek Avatar answered Sep 25 '22 06:09

Bernhard Schussek