Entity getter different situation

75 views Asked by At

I have a Symfony2/AngularJS application and using FOSRestBundle with JMS Serializer Bundle. For normal entities every thing works great but in one of my entities that contains a collection of messages (topic entity) I need to return subset of messages in different situations. For example for the Topic Entity Owner I want to return all messages of the topic for message owner I want to return just the message that posts with the message owner and for other users I don't want to return any messages but they can post a message on topic.

I'm not sure where to implement this. In the topic entity or in the controller or ...

1

There are 1 answers

1
Frank B On

The short answer is: In the controller.

Long version: That you have a Topic entity that holds a collection of messages does not mean that you are not allowed to use just the message entity. Thus in case you don't need a whole topic then you should not use it also. Instead use an (array of the) message entity directly in your controller. You can use the findBy() or findOneBy() function to retrieve the message(s) you need.