FOSRestBundle update only one field at a time using PUT

本小妞迷上赌 提交于 2019-12-25 03:06:44

问题


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());
        }

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.

And here's the Controller

/**
 * 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);
}

回答1:


I am going to answer my own question.

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);
} 

Please find references: Symfony2 REST API - Partial update

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/

Read carefully PATCH vs. PUT chapters.



来源:https://stackoverflow.com/questions/32897913/fosrestbundle-update-only-one-field-at-a-time-using-put

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