我有 2 个实体,Product 和 Product_Types。
他们之间的关系是这样的:
manyToOne:
product:
targetEntity: Product
joinColumn:
name: product_id
referencedColumnName: id
当我添加一个新产品时,我希望能够以相同的形式向这个产品添加类型。
->add('productColor', 'collection', array("type" => new ProductColorType(), "allow_add" => true, 'prototype' => true))
这就是我嵌入表单的方式。
ProductType 和 ProductTypesType 都设置了“data_class”,当我添加一个带有类型的新产品时,一切都很好,除了一件事,symfony/doctrine 没有设置“product_id”列,因此它们之间不会有任何关系我的产品及其类型。
最佳答案
您需要手动交叉链接您的实体。 Symfony 和 Doctrine 都不会为你做这件事。例如,您可以在 add...() 方法中执行此操作:
public function getProductColors()
{
return $this->productColors;
}
public function addProductColor(ProductColor $color)
{
$this->productColors->add($color);
$color->setProduct($this);
}
然后必须满足三个要求才能使其工作:
false
如果你做这一切,你应该没问题。
关于Symfony2 收集表未填写加入栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8748785/