Reputation: 2691
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":"[email protected]"
// 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
Upvotes: 1
Views: 2929
Reputation: 620
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);
Upvotes: 1