好的,所以我目前在3个表上设置了一对多的关系:
食谱-ProductRecipe-产品
###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 / getters / _construct / _tostring。
现在我已完成所有这些设置(它可以自行运行)我希望用户能够从配方表单中将新产品添加到配方中。所以我在食谱表格中制作了一系列表格。
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);
到目前为止,表单已经很好地创建并且可以很好地保存到数据库中。但是在我的Many表中,Recipe_ID字段尚未填充当时正在创建的配方。如何填写该字段?否则,用户正在创建配方并将产品添加到配方中,但是当持久化时,我只需要一个没有产品的新配方和一个未连接到配方的产品ammounts集合。
很抱歉,如果我以一种令人困惑的方式写出来......那是因为我很困惑。
只是为了帮助我一点点添加我的控制器代码:
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;
}
因此,在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(),
));
}
仍无济于事......
这就是现在我的问题是我在newAction中的大部分内容都应该在createAction中。完美的工作。谢谢塞拉德。
答案 0 :(得分:1)
这是一个常见问题。您永远不会调用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);
虽然不直接相关,但请考虑将产品和配方更改为产品和配方。