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.
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:
false
If you do all this, you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With