FOSRestBundle 使用 PUT 一次只更新一个字段 [英] FOSRestBundle update only one field at a time using PUT
问题描述
之前我问过同样的问题 Symfony PUT 不包含映射的所有实体属性,我有一些#hack 解决方案来解决这个问题,但是当表单更复杂,包含选项(数组)或映射实体(对象)时,该解决方案不适用了.所以我想出了非常肮脏的想法来使它工作,例如
While ago I have asked same question Symfony PUT does not contain all entity properties mapped and I've got some #hack solution to solve the problem but when the form is more complex, contains choices (arrays) or mapped entities (objects) the solution is not applicable anymore. So I came up with very dirty ideas to make it work such as
if(is_object($event->getForm()->getData())) $event->setData($event->getForm()->getData()->getId());
if(is_array($event->getData()) && empty($event->getData()))
{
$event->setData([$event->getForm()->getData()]);
}
if($event->getForm()->getData() instanceof \DateTime) $event->setData($event->getForm()->getData()->format('Y-m-d H:i:s'));
if(!is_array($event->getData()) && (is_string($event->getForm()->getData()) || is_integer($event->getForm()->getData())))
{
$event->setData($event->getForm()->getData());
}
但它甚至不完美.所以我必须再问一次是否有更好的解决方案来在发送 json 响应时更新一个值,因为如果我发送 {"user":{"firstName":"John"}}
属于用户表单的所有其他字段都是空的,我无法发送整个资源.我找不到任何解决此问题的方法.
but it's not even working perfect. So I must ask one more time if there's a better solution to updatejust one value at the time of sending json response, because if I send {"user":{"firstName":"John"}}
all other fields belonging to the User form are empty and I cannot send entire resource. I cannot find any solution to this problem.
这里是控制器
/**
* This endpoint updates an existing Client entity.
*
* @ApiDoc(
* resource=true,
* description="Updates an existing Client entity",
* )
* @ParamConverter("user", class="Software:User", options={"mapping": {"user": "guid"}})
*/
public function putAction(Request $request, $user)
{
$form = $this->createForm(new UserType(['userType' => 'client', 'manager' => $this->getDoctrine()->getManager()]), $user, ['method' => 'PUT']);
$form->handleRequest($request);
if($form->isValid())
{
$manager = $this->getDoctrine()->getManager();
$manager->flush();
return $this->view([
'user' => $user
]);
}
return $this->view($form);
}
推荐答案
我要回答我自己的问题.
I am going to answer my own question.
答案是使用 PATCH
方法而不是 PUT.这是修改后的代码:
The answer is to use PATCH
method instead of PUT.
Here's the modified code:
/**
* This endpoint updates an existing Client entity.
*
* @ApiDoc(
* resource=true,
* description="Updates an existing Client entity",
* )
* @ParamConverter("user", class="Software:User", options={"mapping": {"user": "guid"}})
*/
public function patchAction(Request $request, $user)
{
$form = $this->createForm(new UserType(['userType' => 'client', 'manager' => $this->getDoctrine()->getManager()]), $user, ['method' => 'PATCH']);
$form->handleRequest($request);
if($form->isValid())
{
$manager = $this->getDoctrine()->getManager();
$manager->flush();
return $this->view([
'user' => $user
]);
}
return $this->view($form);
}
请查找参考资料:Symfony2 REST API - 部分更新
http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/
http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/
仔细阅读 PATCH 与 PUT 章节.
Read carefully PATCH vs. PUT chapters.
这篇关于FOSRestBundle 使用 PUT 一次只更新一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!