doctrine2:在一对多的双向关系中,如何从逆向保存? [英] doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side?

查看:90
本文介绍了doctrine2:在一对多的双向关系中,如何从逆向保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对一双向关系。

在使用symfony2任务生成crud操作后,当我尝试保存与类别在新/编辑类别表单中,产品不保存...

After generating the crud actions with a symfony2 task, when I try to save the Products associated to a Category in the new/edit Category form, the products are not saved...

namespace Prueba\FrontendBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="category")
 */
class Category
{

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    /**
     * @ORM\Column(name="name")
     */
    protected $name;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function __toString()
    {
        return $this->name;
    }

    public function getProducts()
    {
        return $this->products;
    }

    public function setProducts($products)
    {
        die("fasdf"); //here is not entering
        $this->products[] = $products;
    } 

    public function addProduct($product)
    {
        die("rwerwe"); //here is not entering
        $this->products[] = $product;
    } 
}



namespace Prueba\FrontendBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;


    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name =  $name;
    }

    public function getCategory()
    {
        return $this->category;
    }

    public function setCategory($category)
    {
        $this->category = $category;
    }

    public function __toString()
    {
        return $this->name;
    }
}


推荐答案

它的双向,您需要更新双方的关联。

As its bidirectional you need to update the association on both sides.

将此函数添加到类别实体(如果您喜欢,可以将其称为addChild):

Add this function into the Category Entity (you can call it addChild if you like):

public function addProduct($product)
{
    $this->children->add($product);
}

然后同时更新两个关联:

And then update both associations at the same time:

public function setProductCategory($product_category)
{
    $this->productCategory = $product_category;  
    $product_category->addProduct($this);
}

提示:不要使用$ children / $ parent来描述实体。称它为$ category / $ product,当您想添加另一个父关系时,您将遇到问题。

Tip: Dont use $children / $parent to describe Entities. Call it what it is $category / $product, you'll run into issues when you want to add in another "parent" relationship.

这篇关于doctrine2:在一对多的双向关系中,如何从逆向保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆