S
S
symnoob2020-02-27 11:51:16
symfony
symnoob, 2020-02-27 11:51:16

Symfony - Serializer, Normalizer, Deserializer how to convert Array to Entity Object?

Hello everyone,

does anyone know how to convert an Array to an Entity Object?

the following json is sent:

{
  "data": {
    "object_nr": 565656,
    "title": "My Title",
    "categorie_id": 3
  }
}


How can one correctly and elegantly convert this to an Entity Object?
It's especially not clear how to deal with Relations ...

I tried to do this, and everything works fine, but I suspect that this is "shit-code":
/**
     * @ParamConverter("immo", converter="fos_rest.request_body")
     */
    public function postImmoAction(Immo $immo, Request $request)
    {
        $data = $request->request->all();
        $immo->setCreatedAt(new DateTime());
        $immo->setUpdatedAt(new DateTime());
        $em = $this->getDoctrine()->getManager();

        $categorie = $em->getRepository(Immo\Categorie::class)->find($request->request->get($data['categorie_id']);

        это ManyToOne Realtion:
        $immo->setCategorie($categorie);

        $immo->setUser($this->getUser());


        $em->persist($immo);
        $em->flush();
    }


# what is missing return is understandable, I cut the code so that I didn’t have to read a lot.

Please tell me how you would solve this problem...

Thank you so much!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question