我有两个具有一对多关系的实体
class A {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"update", "read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"create", "update", "read"})
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=B::class, mappedBy="a", cascade={"persist"}, orphanRemoval=true)
* @Groups({"create", "update", "read"})
*/
private $items;
}
class B {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"update", "read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"create", "update", "read"})
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=A::class, inversedBy="items")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $a;
}
当我对此主体执行POST方法时
{
"name": "Name1",
"items": [
{
"name": "Name2"
}
]
}
然后我得到这个答复
{
"id": 1,
"name": "Name1",
"items": [
{
"id": 1,
"name": "Name2"
}
]
}
当我复制请求正文并尝试将ID为1的项目更新为“ Name3”时
{
"id": 1,
"name": "Name1",
"items": [
{
"id": 1,
"name": "Name3"
}
]
}
那我回来了
{
"id": 1,
"name": "Name1",
"items": [
{
"id": 2,
"name": "Name3"
}
]
}
您看到ID为1的项已删除,并且ID为2的新项已创建,但是我需要保留旧项并仅更新name属性。任何想法为什么会这样吗?