Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

php - 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 PruebaFrontendBundleEntity;

use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;

/**
 * @ORMEntity
 * @ORMTable(name="category")
 */
class Category
{

    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORMColumn(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 PruebaFrontendBundleEntity;

use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;

/**
 * @ORMEntity
 * @ORMTable(name="product")
 */
class Product
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMManyToOne(targetEntity="Category", inversedBy="products")
     * @ORMJoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;

    /**
     * @ORMColumn(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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...