2

How can I update FOS USER user details using PATCH method. So when I pass partial details in Json, only these details are updated.

User entity

  /**
   * @ORM\Entity
   * @ORM\Table(name="user")
   */
  abstract class User extends BaseUser
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;

      /**
       * @var string]
       * @ORM\Column(name="first_name", type="string", length=255, nullable=true)
       */
      private $firstName;

      /**
       * @var string
       * @ORM\Column(name="last_name", type="string", length=255, nullable=true)
       */
      private $lastName;

     // getters and setters are here
      }

For example. I have user Jack Nickolson and my json is:

  {"firstName": "John"}

Only first name is updated.

This is my controller. How do I set new parameters to user, without specifing which parameters?

     /**
      * @Route("/update", name="api_user_update")
      * @Security("has_role('ROLE_USER')")
      * @Method("PATCH")
      */
     public function updateAction(Request $request){
         $jsonContent = $request->getContent();
         $params = json_decode($jsonContent);
         $em = $this->getDoctrine()->getEntityManager();
         $response = new JsonResponse();

         $user = $this->getUser();

         // do something to update user details

         $this->get('fos_user.user_manager')->updateUser($user);

         $em->persist($user);
         $em->flush();

         $response->setContent([
        "status" => "user ". $user->getUsername() ." is updated"
         ]);

         return $response;
     }

UPDATE

I tried to use an answer below, so this what I have now

    public function updateAction(Request $request){
    $em = $this->getDoctrine()->getEntityManager();
    $response = new JsonResponse();

    $user = $this->getUser();
    $editForm = $this->createForm('CoreBundle\Form\UserType', $user);

    $requestData = $request->getContent();
    $requestData = json_encode($requestData, true);

    $editForm->submit($requestData, false);

    $this->get('fos_user.user_manager')->updateUser($user);

    $em->persist($user);
    $em->flush();

    $response->setContent([
    "status" => "user ". $user->getUsername() ." is updated"
     ]);

    return $response;
}

Well, my entity was not updated. What am I doing wrong?

Community
  • 1
  • 1
blahblah
  • 1,010
  • 15
  • 40

1 Answers1

2

You need to prepare and create UserType form for partial updated.

Then, when processing a submitted form, you'll need to pass a false $clearMissing option to your form's submit method call in the controller:

$form->submit($request, false);

Thanks to that option the form component will only update the fields passed from the request.

and then flush user:

$em->flush();

Also if you want send data like:

{"firstName": "John"}

your UserType form should have method:

public function getBlockPrefix()
{
    return "";
}

You can also use nice and powerfull https://github.com/FriendsOfSymfony/FOSRestBundle

Kamil Adryjanek
  • 3,238
  • 1
  • 20
  • 21
  • Thanks, but I am not using forms to create or update user. I am creating REST application. Do I still need to have UserType? – blahblah Aug 10 '16 at 14:32
  • You don't have to but based on my experience when creating REST API it is much more convenient to use `Symfony2` form component. – Kamil Adryjanek Aug 10 '16 at 14:35
  • Pleas check for example this article: http://welcometothebundle.com/web-api-rest-with-symfony2-the-best-way-the-post-method/ – Kamil Adryjanek Aug 10 '16 at 14:36
  • Hey, I tried to do what you advised. Created UserType and submitted json data to form. But it didn't work – blahblah Aug 10 '16 at 15:41
  • Please, show me your `UserType` form. Did you use `Symfony2` form component before? You also need to check if the form is valid – Kamil Adryjanek Aug 11 '16 at 04:56