【问题标题】:One-Many-One Symfony2/Doctrine2 - How do I fill the fields in my many relation from the one sideOne-Many-One Symfony2/Doctrine2 - 如何从一侧填充我的多关系中的字段
【发布时间】:2014-04-21 13:33:07
【问题描述】:

好的,所以我目前已经在 3 个表上建立了一对多对一的关系:

食谱-产品食谱-产品

###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 字段未填充当时正在创建的配方。如何在持久性上填写该字段?否则,用户正在创建一个配方并将产品添加到该配方中,但是当我坚持时,我只有一个没有产品的新配方和一组未连接到配方的产品数量。

对不起,如果我以一种令人困惑的方式写了这篇文章......这是因为我自己也很困惑。

编辑

只是为了提供一点帮助,我将添加我的控制器代码,稍微精简一下:

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

所以在 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

现在可以了,我的问题是我在 newAction 中的大部分内容应该在 createAction 中。完美运行。谢谢塞拉德。

【问题讨论】:

    标签: php symfony doctrine-orm entity-relationship


    【解决方案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);
    

    虽然不直接相关,但请考虑将产品和食谱更改为产品和食谱。

    【讨论】:

    • 拥有 $entity->addProduct($products); newAction 中的行现在已将其移至 createAction 并且可以正常工作。最后只花了我一整天的时间来做:(。谢谢你 Cerad 你的明星。现在像你建议的那样整理 Recipe/Product 的复数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 2020-10-20
    相关资源
    最近更新 更多