繁体   English   中英

doctrine2:在一对多的双向关系中,如何从反面保存?

[英]doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side?

我有下面的一对多双向关系。

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

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;
    }
}

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

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

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

然后同时更新这两个关联:

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

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

另一个简短的解决

在您的类别实体中,将此行添加到您的addProduct函数中

  public function addProduct($product)
    {
        $product->setCategory($this); // setting the cateogry of  product 
        $this->products[] = $product;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM