Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the One-to-Many bidirectional relationship below.

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;
    }
}
like image 635
ziiweb Avatar asked May 16 '12 12:05

ziiweb


2 Answers

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

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

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.

like image 156
Lee Davis Avatar answered Oct 18 '22 22:10

Lee Davis


Another short solution :

in your category entity , add this line to your addProduct function

  public function addProduct($product)
    {
        $product->setCategory($this); // setting the cateogry of  product 
        $this->products[] = $product;
    }
like image 26
Yassine CHABLI Avatar answered Oct 19 '22 00:10

Yassine CHABLI