1

I'm trying to deserialize an entity from a JSON file with this method

public function importAnnotationAction($id_rendu)
{
    $em = $this->getDoctrine()->getEntityManager();

    $jsonContent = $this->get("request")->getContent();

    $serializer = $this->get('serializer');
    $annotationEntity = $serializer->deserialize($jsonContent, 'Alecsia\AnnotationBundle\Entity\Annotation', 'json');

    //Save entity
    $em->persist($annotationEntity);
    $em->flush();

    return new Response($jsonContent . "\n" . var_dump($annotationEntity));
}

the JSON file used is from a same entity

and the output entity fields are all set to null :

object(Alecsia\AnnotationBundle\Entity\Annotation)#568 (18) { ["id":protected]=> int(26) ["fichier":protected]=> NULL ["rendu":protected]=> NULL ["modele":protected]=> NULL ["debut_ligne":protected]=> int(0) ["debut_col":protected]=> int(0) ["fin_ligne":protected]=> int(0) ["fin_col":protected]=> int(0) ["nom":protected]=> NULL ["commentaire":protected]=> NULL ["valeur":protected]=> NULL ["relatif":protected]=> NULL ["exercice":protected]=> NULL ["deleguer_nom":protected]=> bool(false) ["deleguer_commentaire":protected]=> bool(false) ["deleguer_valeur":protected]=> bool(false) ["deleguer_relatif":protected]=> bool(false) ["deleguer_exercice":protected]=> bool(false) }

and the entity id increment with each call, what am I doind wrong?

PeterFour
  • 329
  • 4
  • 16
  • Can you paste the request body? – Peter Bailey Apr 10 '14 at 17:26
  • @PeterBailey the JSON data ? – PeterFour Apr 10 '14 at 17:29
  • Yes. I'm curious about what is actually ending up in `$jsonContent` – Peter Bailey Apr 10 '14 at 17:45
  • @PeterBailey here is the json content : http://pastebin.com/sV0W53E3 – PeterFour Apr 10 '14 at 17:55
  • Ok, so is this a POST request then? Have you 100% verified that `$jsonContent` has that content? What about your `Annotation` entity - does it have a public setter for each of those properties? – Peter Bailey Apr 10 '14 at 18:04
  • @PeterBailey for the jsonContent it's from the var dump, and I think there is a setter for every fields, but some fields name use underscore and setter use CamelCase – PeterFour Apr 10 '14 at 18:06
  • The underscore vs camelCase issue could be a problem, but I don't know the nuts and bolts of how that serializer bundle works i.e., what type of field-name conversions it attempts it order to match a target property/setter on the outbound object. – Peter Bailey Apr 10 '14 at 20:12
  • @PeterBailey I try something new : import another entity, and the bundle told me that there was missing annotation (@Type) So I set them all in every entity file. But then I have an error : `Warning: spl_object_hash() expects parameter 1 to be object, integer given` for the other entity and the Annotation export still failed – PeterFour Apr 10 '14 at 20:15
  • @PeterBailey But does every protected field need to have its own setter? If so I'll try and look if there is any one missing – PeterFour Apr 11 '14 at 01:23
  • 1
    If there is a issue with underscore and CamelCase you can use `use JMS/Serializer/Annotation as Serializer` and then above `setSomeKindOfField()` add the annotation `@Serializer\SerializedName('some_kind_of_field')`. Alternatively you could add it to you YAML if you go swing that way. – qooplmao Apr 11 '14 at 23:54
  • (Windows Notepad issue) Please, consult this, I shared the problem too and it fixed it: http://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before-doctype – Felix Aballi Sep 19 '14 at 14:52

0 Answers0