1

I have a User entity that contains address. I will save address as a json in my database. After form validation, I have to manually serialize address before persisting data. Is there anyway to avoid doing it like that ? Is it possible to call serialize event when doctrine is persisting data ?

class User{
    /**
    * @ORM\Column(name="username", type="string", length=30)
    **/
    private $username;

    /**
    * @ORM\Column(name="address", type="json")
    **/
    private $address;
}

class Address{
   private $postalcode;
   private $street;
}

// Inside my controller

class UserController extends Controller{

    /**
     * @Rest\View(StatusCode = Response::HTTP_CREATED)
     *
     * @Rest\Post(
     *   path = "/user",
     *   name = "user_create"
     * )
     */
      public function createAction(){
         $user = new User();
         $form = $this->createForm(UserType::class, $user);
         $form->submit($request->request->all());

         if ($form->isValid())
         {
            $em = $this->getDoctrine()->getManager();
            $user->setAddress($this->get('jms_serializer')->serialize($user->getAddress(), 'json'));

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

            return $this->view($user, Response::HTTP_CREATED);
         }

         return $form;
      }
}
John
  • 107
  • 1
  • 9
  • if you configure Doctrine to use json, it will parse by its own. If you want to do it manually, you must use text for doctrine and parse it everytime by your own. – fucethebads Jun 27 '18 at 09:09
  • @fucethebads, doctrine is saving field as empty when the serialize line is commented in controller – John Jun 27 '18 at 09:13
  • ah okay, get it. Doctrine cann only parse arrays. so i would say you have to define the field as text and un-/serialize it by your self – fucethebads Jun 27 '18 at 10:00
  • 1
    What about using Doctrine Event Listener for doing the serialization? you will have to implement the serialization manually but only once. https://symfony.com/doc/current/doctrine/event_listeners_subscribers.html – Marçal Berga Jun 27 '18 at 10:42
  • Why don't you use `$form->handleRequest($request)`? You shouldn't perform serializing in your controller. Doctrine should handle this. And if you really want this, look into Event Listeners or Data Transformers. Symfony follows the philosophy of "thin controllers and fat models". – Stephan Vierkant Jun 27 '18 at 10:43
  • @StephanVierkant I have to submit form manually (data are posted through ajax in json format and I want to use form to validate fields). I know I should not do serialization in controller, but when I left like that doctrine returns empty json. Based on documentation, doctrine is using PHP's JSON encoding functions so a sample dump(json_encode($user->getAddress())) also returns empty json. I don't know if there is a way to tell doctrine to use jms serializer – John Jun 27 '18 at 11:37
  • @MarçalBerga thanks I will go it – John Jun 27 '18 at 11:38

1 Answers1

0

Doctrine failed to make it json because address property was private (PHP json_encode returning empty sctructure). Making it public resolve the problem. When defining type as json, doctrine will use php's json encoding functions: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#json-array Thanks

John
  • 107
  • 1
  • 9