Xavier
Xavier

Reputation: 109

Deserializing doctrine references with JMSSerializer

I'm not sure how to write the title properly since I'm not sure what I'm looking for, but basically what I'm wondering if there's a way to deserialize the following json:

{ "title": "hello world", "user": 1 }

But since user is a ORM mapping, attempt to find the entity from it's repository instead of trying to parse it directly.

I can easily do this for a specific field by using a Handler for that case, pass along the EM and look the entity up, but I was hoping to come up with a more general solution where any relationship can be specified either by it's serialized fields (in order to create a new entity) or by it's id in order to lookup an existing one.

Upvotes: 2

Views: 1729

Answers (1)

Max Małecki
Max Małecki

Reputation: 1702

JMSSerializer bundle requires to add an extra annotation

/**
 * @ORM\OneToMany(targetEntity="Test", mappedBy="myentity", fetch="LAZY", cascade={"persist"})
 * @ORM\OrderBy({"dateTo" = "DESC"})
 * @Expose
 * @Type("ArrayCollection<Acme\Bundle\Entity\Test>")
 */

Upvotes: 1

Related Questions