Doctrine relation write NULL in table

ε祈祈猫儿з 提交于 2019-12-11 15:37:31

问题


My 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

When I want to send a POST request, in the form of a JSON array, everything seems to be written, but except for one field: item_id, NULL is written there. For deserialization I use JMSSerializer. What could be the problem? Entities getters and setters are standard. JSON array:

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

In MySQL table:

+----+---------+-------+--------------+
| 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  |
+----+---------+-------+--------------+

来源:https://stackoverflow.com/questions/49220634/doctrine-relation-write-null-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!