0

I have two entities: User and UserInfo:

class User
{

    ...

    /**
     * @ORM\OneToOne(targetEntity="UserInfo")
     * @ORM\JoinColumn(name="userinfo_id", referencedColumnName="id_user")
     * @Serializer\Groups({"o", "i-self-editUser"})
     */
    private $userInfo;

    ...

}

class UserInfo
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_user", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="users_id_user_seq", allocationSize=1, initialValue=1)
     */
    private $idUser;

    ...

}

And I'm trying to deserialize a User sent by means of JMSSerializer and its Doctrine Contructor. Everything works fine if UserInfo is not specified. User is loaded from the DB and the fields sent are updated:

By sending:

{
    "username": "test@test.us",
    "email": "test@test.us",
    "name": "test",
    "lang": "en-US"
}   

What I get deserialized is User's and UserInfo's well loaded.

But if I try to send something like this:

{
    "username": "test@test.us",
    "email": "test@test.us",
    "name": "test",
    "lang": "en-US"
    "user_info": {
        "short_date_format": "Y-m-dd"
    }
}   

short_date_format is updated and serialized, but all the other fields will not be loaded from the DB, setting all of them to null. This is not the behavior I would like to get. How can I fix this?

Update

I thought I'd better patch a single "nesting level" if I want to do it properly. The URL could resemble this (FOSRestbundle controller's annotation):

* @Patch("/users/{username}/userInfo", requirements={"username"=".+(\.)?\w+"})

this way, I can patch userInfo by following Ocramius' piece of suggestion. Can anyone give me some feedback about this? Do you think it could be a good/best practice in order to implement a decent patch?

Community
  • 1
  • 1
Bertuz
  • 2,390
  • 3
  • 25
  • 50

1 Answers1

0

JMSSerializer is not written to keep your entities as much similar to database as possible. You can make simple serialization or desrialization. There are some examples of code where users create some factories to achieve your goal.

Have you tried merge your deserialized entity into EntityManager? It should stick to your needs.

michail_w
  • 4,318
  • 4
  • 26
  • 43
  • merge actually produces the same result. Maybe because it treats the deserialized "null" as a new value to overwrite? Can you please provide me some example of factories to get some inspiration? Thank you – Bertuz Sep 26 '16 at 23:07
  • could you give me some feedback about my updated question? Thank you – Bertuz Sep 27 '16 at 00:19
  • Do you seralize/deserialize doctrine entities only? Check accepted answer from this post http://stackoverflow.com/questions/16525849/how-to-update-symfony2-doctrine-entity-from-a-groups-inclusion-policy-jmsserial if you do. – michail_w Sep 27 '16 at 10:48