学说中的塞特斯关系

时间:2018-03-12 20:59:13

标签: symfony doctrine-orm doctrine getter-setter relationships

我的Item.orm.yml:

AppBundle\Entity\Item:
    type: entity
    manyToMany:
        categories:
            targetEntity: AppBundle\Entity\Category
            cascade: ['persist']
            inversedBy: items
            joinTable:
                name: item_category
                joinColumns:
                    item_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    category_id:
                        referencedColumnName: id
    oneToMany:
        attributes:
            targetEntity: AppBundle\Entity\AttrValue
            mappedBy: item
            cascade: ['persist','remove']
    table: items
    repositoryClass: AppBundle\Repository\ItemRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255

AttrValue.orm.yml:

AppBundle\Entity\AttrValue:
    type: entity
    manyToOne:
        item:
            targetEntity: AppBundle\Entity\Item
            inversedBy: attributes
            cascade: ['persist','remove']
            joinColumn:
                name: item_id
                referencedColumn: id
                nullable: false
        attribute:
            targetEntity: AppBundle\Entity\Attribute
            inversedBy: attrValues
            cascade: ['persist','remove']
            joinColumn:
                name: attribute_id
                referencedColumn: id
                nullable: false
    table: attr_value
    repositoryClass: AppBundle\Repository\AttrValueRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        value:
            type: string
            length: 255

Attribute.orm.yml:

AppBundle\Entity\Attribute:
    type: entity
    oneToMany:
            attrValues:
                targetEntity: AppBundle\Entity\AttrValue
                mappedBy: attribute
                cascade: ['persist','remove']
    table: attribute
    repositoryClass: AppBundle\Repository\AttributeRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        alias:
            type: string
            length: 255
        name:
            type: string
            length: 255

当我想以JSON数组的形式发送POST请求时,似乎所有内容都被写入,但除了一个字段:item_id之外,还写入了NULL。对于反序列化,我使用JMSSerializer。可能是什么问题呢?实体getter和setter是标准的。 JSON数组输入:

{
    "name": "Book",
    "categories": [
        {
            "id": 2
        },
        {
            "id": 11
        }
    ],
    "attributes": [
        {
            "value": "black",
            "attribute": {
                "alias": "color",
                "name": "color"
            }
        },
        {
            "value": "1",
            "attribute": {
                "alias": "price",
                "name": "price"
            }
        }
    ]
}

在attr_value表中:

+----+---------+-------+--------------+
| id | item_id | value | attribute_id |
+----+---------+-------+--------------+
| 1 |    NULL | 6     |           8   |
| 2 |    NULL | black |           9   |
| 3 |    NULL | 6     |           10  |
| 4 |    NULL | bk    |           11  |
| 5 |    NULL | 1     |           12  |
+----+---------+-------+--------------+

我认为我需要为设置者添加一些关系,但我不知道该怎么做(我已经撕毁谷歌,但没有找到任何东西)。

更新 当我尝试发送类似的东西时,一切正常(但不正确),但我不需要这些:

{
    "name": "Book",
    "categories": [
        {
            "id": 2
        },
        {
            "id": 11
        }
    ],
    "attributes": [
        {
            "value": "black",

            "attribute": {
                "alias": "color",
                "name": "color"
            }
        },
        {
            "value": "1",
            "item": {       #<----this line
            "name": "Book"  #<----and this
            },
            "attribute": {
                "alias": "price",
                "name": "price"
            }
        }
    ]
}

0 个答案:

没有答案