Ber*_*Nes 6 doctrine-orm fosrestbundle jmsserializerbundle symfony-2.8
我有一个带有fos restbundle的Symfony rest api构建,我正在反序列化json PUT请求,以便更新具有to-many关系的doctrine实体.但是,配置的to-many子对象orphanremoval=true在json数据中不存在时不会从数据库中删除.
PUT请求有效负载:
{
"id": 1,
"name":"Some name",
"export_destinations": [
{
"id": 1,
"type": "USER_STORAGE",
"user": {"id": 5}
}
{
"id": 2,
"type": "SYSTEM_STORAGE"
}
]
}
Run Code Online (Sandbox Code Playgroud)
控制器动作:
/**
* @Rest\Put("{id}")
* @ParamConverter(
* "exportJob",
* converter="fos_rest.request_body",
* options={"deserializationContext"={"groups"={"put"}}}
* )
* @Rest\View(serializerGroups={"details"})
* @param ExportJob $exportJob
* @return ExportJob
*/
public function putAction(ExportJob $exportJob)
{
$this->getManager()->persist($exportJob);
$this->getManager()->flush();
return $exportJob;
}
Run Code Online (Sandbox Code Playgroud)
ExportJob实体
/**
* @ORM\Entity()
*/
class ExportJob
{
/**
* @var ArrayCollection|ExportDestination[]
*
* @ORM\OneToMany(targetEntity="ExportDestination", mappedBy="exportJob", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
protected $exportDestinations;
/**
* @param ExportDestination $exportDestination
* @return $this
*/
public function addExportDestination(ExportDestination $exportDestination)
{
$exportDestination->setExportJob($this);
$this->exportDestinations->add($exportDestination);
return $this;
}
/**
* @param ExportDestination $exportDestination
* @return $this
*/
public function removeExportDestination(ExportDestination $exportDestination)
{
$this->exportDestinations->removeElement($exportDestination);
$exportDestination->setExportJob(null);
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
JMS元数据
MyProject\ExportBundle\Entity\ExportJob:
exclusion_policy: ALL
properties:
id:
groups: ['list', 'details', 'put']
expose: true
name:
groups: ['list', 'details', 'put', 'patch', 'post']
expose: true
exportDestinations:
groups: ['details', 'put', 'patch', 'post']
expose: true
type: 'ArrayCollection<MyProject\ExportBundle\Entity\ExportDestination>'
Run Code Online (Sandbox Code Playgroud)
我正在使用DoctrineObjectConstructor
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
Run Code Online (Sandbox Code Playgroud)
现在,当我从json有效负载中的export_destinations数组中省略第二个对象时,在反序列化后,控制器操作中的exportJob在数组集合中只有一个exportDestination对象.但是当我坚持下去时,我希望doctrine从数据库中删除exportDestination,因为我有orphanremoval=true.
我认为问题是,removeExportDestination()在反序列化期间永远不会调用该方法,应该在反向端将关系设置为null.如果没有发生,它将不会删除实体,因为它还没有成为孤儿.
有没有一种方法JMS将在反序列化期间使用ArrayCollections的添加/删除方法?
我也试过用merge()而不是persist()没有任何区别
您对“在反序列化期间永远不会调用removeExportDestination()方法”的看法是正确的。
您可以定义访问器属性来执行您想要的操作:
exportDestinations:
groups: ['details', 'put', 'patch', 'post']
expose: true
type: 'ArrayCollection<AppBundle\Entity\ExportDestination>'
accessor:
getter: "getExportDestinations"
setter: "setExportDestinations"
Run Code Online (Sandbox Code Playgroud)
在 ExportJob 实体中:
public function getExportDestinations()
{
return $this->exportDestinations;
}
public function setExportDestinations($exportDestinations)
{
// first detach existing related entities.
foreach ($this->exportDestinations as $exportDestination) {
$exportDestination->setExportJob(null);
}
$this->exportDestinations = $exportDestinations;
foreach ($exportDestinations as $exportDestination) {
$exportDestination->setExportJob($this);
}
}
Run Code Online (Sandbox Code Playgroud)
以便在反序列化期间调用“setExportDestinations”并负责关系删除。
话虽如此,我不确定你是否应该使用orphanremoval=true关系,因为
orphanRemoval=true,即使您将从一个 ExportJob 中删除给定的 ExportDestination,然后附加到另一个 ExportJob,该 ExportDestination 也会在持久期间被删除,因为引用已被删除。
我建议删除它并找到另一种方法来删除“孤立”ExportDestination 实体。