现在我已经在3个表上建立了一个一对多对一的关系:
Recipes-ProductRecipe-Products
###Recipes.yml###
oneToMany:
products:
targetEntity: ProductRecipe
mappedBy: recipes
cascade: ["all"]
###ProductRecipe.yml###
manyToOne:
products:
targetEntity: Products
inversedBy: recipes
recipes:
targetEntity: Recipes
inversedBy: products
###Products.yml###
oneToMany:
recipes:
targetEntity: ProductRecipe
mappedBy: products
cascade: ["all"]
我已经使用内置命令(doctrine:generate:entities)设置了这些实体,所以我有了所有的setter/getter/_construct/_tostring。
现在我已经设置好了所有这些(它自己可以很好地工作),我希望用户能够从配方表单向配方添加新产品。因此,我在recipe表单中创建了一个表单集合。
class RecipesType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('recipename')
->add('recipedesc')
->add('recipeyeild')
->add('recipecost');
$builder->add('products', 'collection', array(
'type' => new ProductRecipeType(),
'allow_add' => true,));
}
连接到这个表单:
class ProductRecipeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ammount')
->add('products')
;
}
在我的食谱控制器中,我有这个:
public function newAction()
{
$entity = new Recipes();
$pr = new ProductRecipe();
$entity->getProducts()->add($pr);
$form = $this->createCreateForm($entity);
到目前为止,表单创建得很好,保存到数据库中也很好。然而,在我的许多表中,Recipe_ID字段没有填充当时正在创建的配方。如何在持久化中填充该字段?否则,用户将创建一个配方并向配方中添加产品,但是当持久化时,我只得到一个没有产品的新配方和一个未连接到配方的产品数量集合。
很抱歉,如果我把这个写得很混乱…那是因为我自己也很困惑。
编辑
只是为了帮助一点,我添加了我的控制器代码修剪了一下:
class Recipes
{
private $id;
private $recipename;
private $recipedesc;
private $recipeyeild;
private $recipecost;
public function __construct()
{
$this->products = new 'Doctrine'Common'Collections'ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function addProduct('BC'InventoryBundle'Entity'ProductRecipe $pr)//<-this was $products
{
//$this->products[] = $products; <-Original code now changed to below as suggested.
$this->products->add($pr);
$pr->setRecipes($this);
return $this;
}
public function removeProduct('BC'InventoryBundle'Entity'ProductRecipe $products)
{
$this->products->removeElement($products);
}
public function getProducts()
{
return $this->products;
}
和我的ProductRecipe实体:
class ProductRecipe
{
private $id;
private $ammount;
private $products;
private $recipes;
public function getId()
{
return $this->id;
}
public function setAmmount($ammount)
{
$this->ammount = $ammount;
return $this;
}
public function getAmmount()
{
return $this->ammount;
}
public function setProducts('BC'InventoryBundle'Entity'Products $products = null)
{
$this->products = $products;
return $this;
}
public function getProducts()
{
return $this->products;
}
public function setRecipes('BC'InventoryBundle'Entity'Recipes $recipes = null)
{
$this->recipes = $recipes;
return $this;
}
public function getRecipes()
{
return $this->recipes;
}
<标题>编辑2 h1> Cerad的建议下,我得到了这些…
//Recipes entity
public function addProduct('BC'InventoryBundle'Entity'ProductRecipe $products)
{
$this->products->add($products);
$products->setRecipes($this);
return $this;
}
//Recipes Controller
public function newAction()
{
$entity = new Recipes();
$products = new ProductRecipe();
$entity->getProducts()->add($products);
$entity->addProduct($products);
$form = $this->createCreateForm($entity);
return $this->render('BCInventoryBundle:Recipes:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
仍然无济于事……
<标题> 3 编辑This Now works我的问题是我在newAction中所拥有的大部分内容应该在createAction中。完美的工作。谢谢你,杰拉尔德。
标题>标题>这是一个常见的问题。您从未调用ProductRecipe::setRecipe()方法。可能也不会调用setProduct
class Recipe
{
public function addProduct($pr)
{
$this->products->add($pr);
$pr->setRecipe($this); // *** This is what you need to add
在你的控制器中:
$entity->addProduct($pr);
尽管不直接相关,但考虑将产品和配方更改为产品和配方。