【问题标题】:doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side?教义2:在一对多的双向关系中,如何从反面保存?
【发布时间】:2012-05-16 12:28:14
【问题描述】:

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

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

【问题讨论】:

    标签: php symfony doctrine doctrine-orm


    【解决方案1】:

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

    将此函数添加到Category Entity中(如果你喜欢,你可以称它为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”运行 $product->setProductCategory($this),您也可以反过来。取决于您的工作环境。
    • 我在创建问题时没有粘贴正确的类。现在可以了,你能再看一下吗?
    • 是的,同样的事情也适用,双向需要在两端更新。在 Category::addProduct($product) 上,添加行 $product->setCategory($this);你应该准备好了。
    【解决方案2】:

    另一个简短的解决方案:

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

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

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多