【问题标题】:FOSRestBundle update only one field at a time using PUTFOSRestBundle 使用 PUT 一次只更新一个字段
【发布时间】:2015-10-01 22:38:55
【问题描述】:

之前我问过同样的问题Symfony PUT does not contain all entity properties mapped 并且我有一些#hack 解决方案来解决问题,但是当表单更复杂时,包含选项(数组)或映射实体(对象)时,解决方案不适用了。所以我想出了一些非常肮脏的想法来让它发挥作用,比如

        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"}} 属于用户表单的所有其他字段都是空的,我无法发送整个资源。我找不到任何解决此问题的方法。

这是控制器

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

【问题讨论】:

    标签: rest symfony fosrestbundle


    【解决方案1】:

    我将回答我自己的问题。

    答案是使用PATCH 方法而不是PUT。 这是修改后的代码:

    /**
     * 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 - 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/

    仔细阅读 PATCH 与 PUT 章节。

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 2013-07-27
      • 2023-03-15
      • 2016-10-09
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多