ovesco
ovesco

Reputation: 633

JMSSerializerBundle - only serialize ID in doctrine relation

For example, let's say that I have two entities, Article and Comment, comments mapped to articles through an unidirectional manyToOne relation. What I want to know is, if it's possible to serialize relation, but in the mappedOne, only serialize the ID.

For example, if I serialize an article, I'll get a complete json representation of it (including id, title, content...).

{"title":"article title", "content":"article content", "id":"7"}

But if I serialize a comment, I want it to hava a complete json representation of itself, but in the article field, I only want the article id.

{"id":"2", "author":"foo","content":"comment content","linked_article":{"id":"7"}}

Thanks a lot !

Upvotes: 1

Views: 1553

Answers (1)

mickadoo
mickadoo

Reputation: 3483

In the controller action (getCommentAction) do you have serializer groups in the annotation?

I think that by putting this annotation over the controller action

@Rest\View(serializerGroups={"id","comment"}

And in your Article entity putting

/**
 * @JMS\Serializer\Annotation\Groups({"id", "article"})
*/
protected $id;

It should work.

Upvotes: 1

Related Questions