Doctrine 2 is many for many (Products - Categories)

Hi I have many, many relationships between Items (Products) and categories, and I implemented these three objects:

  • Object Object:

    /** * @Entity * @Table(name="items") */ use Doctrine\Common\Collections\ArrayCollection; class Item { /** * * @Id @Column(type="integer") * @GeneratedValue */ private $id_item; /** @OneToMany(targetEntity="ItemCategories", mappedBy="item") */ protected $categories; public function __construct() { $this->categories=new ArrayCollection(); } public function addCategory(ItemCategories $category){ $this->categories->add($category); } public function getCategories(){ return $this->categories; } } 

    2 Attach a table (ItemCategories)

      /** * @Entity * @Table(name="item_categories") */ class ItemCategories { /** * * @Id @Column(type="integer") * @GeneratedValue */ private $id; /** * @Column(type="integer") */ private $id_item; /** * @Column(type="integer") */ private $id_category; /** @ManyToOne(targetEntity="Category", inversedBy="ItemCategories") * @JoinColumn(name="id_category", referencedColumnName="id_category") * */ protected $category; /** @ManyToOne(targetEntity="Item", inversedBy="$categories") * @JoinColumn(name="id_item", referencedColumnName="id_item") * */ protected $item; public function getCategory() { return $this->category; } public function setCategory($category) { $this->category = $category; } public function getItem() { return $this->item; } public function setItem($item) { $this->item = $item; } } 

    Table 3.Categories

      /** * @Entity * @Table(name="categories") */ class Category { /** * * @Id @Column(type="integer") * @GeneratedValue */ private $id_category; /** @OneToMany(targetEntity="ItemCategories", mappedBy="category") */ protected $ItemCategories; /** * * @Column(type="string") @var string */ } 

Now my problem is that I do not know how to insert an element using EXISTING categories. I tried:

  $item= new Entity\Item(); $itemCategoriesReferences=new Entity\ItemCategories(); $productCategoriesReferences->setItem($product); //get existing category from db using PkId $itemCategoriesReferences->setCategory($CategoryModel->getCategory(1)); $item->addCategory(itemCategoriesReferences); 

I know this doesn't really matter, but I have no other idea, so please help me.

thanks

0
source share
2 answers

The third object is useful if you want to set some properties of the relationship between a category and an element. For example, the item "belongs" to a category, the item is "proposed" to be added to the category, the item "expects" to be removed from the category. Since your ItemCategories does not display any of these properties, it is best for you to do this as described in the manual. Read more about this here: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional

+1
source

You must first rename ItemCategories to ItemCategory, since an instance of this item is only a link between 1 item and 1 category.

Then it is simple:

 $item = new Item(); $em->persist($item); $category = $em->getRepository('category')->find($id_category); $itemCategory =new ItemCategory(); $itemCategory->setItem($item); $itemCategory->setCategory($category); $em->persist($itemCategory); 
0
source

Source: https://habr.com/ru/post/951453/


All Articles