Symfony中的ManyToMany关系

时间:2018-03-02 10:04:25

标签: php symfony doctrine-orm doctrine yaml

我有两个实体:类别和产品,一个产品可以参考各种类别。我在Doctrine中创建了ManyToMany关系,这就是我得到的(代码I'我将在下面添加)。有一个表格包含项目(ID,名称,价格等),一个包含类别(id,name,child_id)的表格以及链接它们的表格(id_item,id_categories)。我需要将请求者作为类型的json数组发送:

{
"name": "item1",
"price": 250,
"color": "red",
"size": 23
"weight": 45,
#And there point to which categories belong
}

所以我不明白如何实现这一点。如果你单独执行此操作,只需将产品ID和类别ID随机播放到链接表中,一切都将正常工作并显示,这是一个响应示例(显示所有项目):

{
    "id": 1,
    "name": "bike",
    "price": 200,
    "color": "red",
    "size": 150,
    "weight": 15,
    "category": [
        {
            "id": 1,
            "name": "parent"
        },
        {
            "id": 2,
            "name": "child to 1",
            "parent": {
                "id": 1,
                "name": "parent"
            }
        }
    ]
}

请告诉我,我该怎么做?以便减少麻烦...以下是我的实体: 物品实体:

class Item
{
    private $id;
    private $name;
    private $price;
    private $color;
    private $size;
    private $weight;
    protected $category;
//getters and setters
}

类别实体:

class Category
{
    private $id;
    private $name;
    private $parent;
    private $children;
    protected $items;
    public function __construct()
    {
        $this->children = new ArrayCollection();
        $this->items = new ArrayCollection();
    }
 //getters and setters
}

Item.orm.yml:

AppBundle\Entity\Item:
    type: entity
    manyToMany:
        category:
            targetEntity: AppBundle\Entity\Category
            cascade: ['persist']
            inversedBy: items
            joinTable:
                name: item_category
                joinColumns:
                    item_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    category_id:
                        referencedColumnName: id
    table: items
    repositoryClass: AppBundle\Repository\ItemRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
        price:
            type: integer
        color:
            type: string
            length: 255
            nullable: true
        size:
            type: integer
        weight:
            type: integer

Category.orm.yml:

AppBundle\Entity\Category:
  type: entity
  oneToMany:
        children:
            targetEntity: AppBundle\Entity\Category
            mappedBy: parent
            #orderBy:
                #name: ASC
  manyToOne:
        parent:
            targetEntity: AppBundle\Entity\Category
            inversedBy: children
            joinColumn:
                name: parentId
                referencedColumn: id
  manyToMany:
        items:
            targetEntity: AppBundle\Entity\Item
            mappedBy: category
            cascade: ['persist']
  table: category
  repositoryClass: AppBundle\Repository\CategoryRepository
  id:
      id:
          column: id
          type: integer
          id: true
          generator:
          generator:
              strategy: AUTO
  fields:
      name:
          type: string
          lenght: 255

更新 我了解到我需要将变量$ category,到ArrayCollection,并创建一个新的setter来添加一个类别,然后Request数组应该是这样的:

{   
    "name": "board",
    "price": 2,
    "color": "white",
    "size": 15,
    "weight": 1,
    "categories": 
        [
            {"id": 1},
            {"id": 3}
        ]
}

但是如何让setter记录ArrayCollection的类别? 我的控制器:

public function postItemAction(Request $request)
{
    $serializer = $this->get('jms_serializer');
    $content = $request->getContent();
    $item = $serializer->deserialize($content,'AppBundle\Entity\Item','json');
        $em = $this->getDoctrine()->getManager();
        $em->persist($item);
        $em->flush();
    return new View($item,Response::HTTP_OK);
}

0 个答案:

没有答案