2012-01-05 50 views
5

我有2个实体Product和Product_Types。Symfony2 Collection Form不填充连接列

这是它们之间的关系:

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

当我添加一个新的产品,我希望能够给类型添加到该产品具有相同的形式。

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

这就是我如何嵌入窗体。

'data_class'是为ProductType和ProductTypesType设置的,当我添加一个带有类型的新产品时,一切都很好,除了一件事情,symfony/doctrine没有设置'product_id'列,所以会有在我的产品和它的类型之间没有关系。

+0

这很有趣,在我问这个问题之前,我一直在寻找一个解决方案,然后我找到了解决方案,我在产品坚持之前就为这些类型设置了产品。它解决了这个问题,但我仍然认为不应该这样做。 – Tom 2012-01-05 19:57:08

回答

0

尝试在实体管理器刷新之前也坚持productColors。

5

您需要手动将您的实体交叉链接。 Symfony和Doctrine都不会为你做这件事。您可以外接内,例如做这个...()方法:

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

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

然后三个要求必须以满足这个工作:

  • 你正在运行的Symfony主
  • 您正在运行原则2.1.7/2.2.3
  • 你的 “收藏” 栏的 “by_reference” 选项设置为false

如果你这样做,你应该没问题。

+0

在addProductColor()中调用setProduct()会起作用。但是,根据文档,将by_reference设置为false应该调用setProductColors(),但似乎并非如此。任何想法? – 2013-02-27 15:58:05

+0

取决于添加/删除方法是否可用。如果他们是,他们被用来代替setter。 – 2013-04-17 23:14:52