doctrine 2 many to many (Products - Categories)

后端 未结 2 446
暖寄归人
暖寄归人 2021-01-26 09:05

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

  1. Item Entity:

       /**
     * @         
    
    
            
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 09:39

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

提交回复
热议问题