1

I would like to map a json string to my entity Poste.

Here is my json output with var_dump :

string(695) "{
              "id":2,
              "user": {
                  "id":1,
                  "username":"tom.smith",
                  "email":"tom.smith@gmail.com"
                  // other properties of User class (FOSUserBundle)
                 },
              "description":"cool",
              "nb_comments":0,"nb_likes":0,
              "date_creation":"2014-04-13T20:07:34-0700", 
              "is_there_fashtag":false,
              "fashtags":[]
            }"

I tried to deserialize with jmsserializer :

$postes[] = $serializer->deserialize($value, 'Moodress\Bundle\PosteBundle\Entity\Poste', 'json');

I have an error :

Catchable Fatal Error: Argument 1 passed to 
     Moodress\Bundle\PosteBundle\Entity\Poste::setUser() must be an instance of
     Moodress\Bundle\UserBundle\Entity\User, array given

Why am I not able to deserialize a simple entity poste ?

Thanks

manonthemoon
  • 2,611
  • 8
  • 26
  • 40
  • did you map your entities with the correct expose/type methods from JMS Serializer? http://jmsyst.com/libs/serializer/master/reference/annotations#type – coma Apr 14 '14 at 21:29

1 Answers1

1

Your $user property in Moodress\Bundle\PosteBundle\Entity\Poste refers to Moodress\Bundle\UserBundle\Entity\User. The setUser() accepts only a User object, not an array.

I guess you should first create a $user with the 'user' array and then pass it to your poste object. In this situation, it seems hard to do it in another way than the one I wrote here.

$data = json_decode($yourJsonFile, true);

$user = new User();
$user->setId($data['user']['id']);
$user->setUsername($data['user']['username']);
// …

$poste = new Poste();
$poste->setId($data['id']);
$poste->setDescription($data['description']);
// …
$poste->setUser($user);
Community
  • 1
  • 1
Einenlum
  • 600
  • 4
  • 15