我的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"
}
}
]
}
在MySQL表中:
+----+---------+-------+--------------+
| 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 |
+----+---------+-------+--------------+