doctrine 2 many to many (Products - Categories)

2019-03-05 05:26发布

Hi I have a many to many relation between Items(Products) and categories and I implemented using these three entities:

  1. Item Entity:

       /**
     * @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 Join 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;
            }
    
    
    
        }
    

    3.Categories Table

     /**
     * @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 don't know how to insert an item 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 that it does not make much sens but I don't have any another idea, so please help me out.

Thanks

2条回答
劫难
2楼-- · 2019-03-05 06:03

A 3rd entity is only useful if you want to set some properties of the Relation between Category and Item. For example Item "belongs" to Category, item is "suggested" to be added to category, item is "pending" deletion from category. Since your ItemCategories doesn't show any of such properties you are really best of doing it the way the manual specifies. Read more about it here: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional

查看更多
我只想做你的唯一
3楼-- · 2019-03-05 06:03

First, you should rename ItemCategories by ItemCategory, since an instance of this item is only the link between 1 item and 1 category.

Then it's 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);
查看更多
登录 后发表回答