0

I have a Symfony 3.4 projet with a REST api. I use JMS serializer.

I have a entity User and I have a route /api/user which return the user id, name , ...

I also have a entity badges which has a relation many to many with user (so a user_badge table). Like I read, when the pivot table have extra column (like in my case on user_badge), I need to create two relation many to one to link my user to badges.

In my route /api/user I add the return on my badges with JMS, I return my badge id and the achievement date (the extra column) from user_badge with the method getUserBadges from my entity User.

But now I want to order by the badges using a column from the badge entity.

How can I achieve this ? The fact than my model user can't access the badges without a heavy foreach. I need to make a request to getting all the badges in the correct order and passing this to JMS.

(I don't know which source file I should provide, cause I don't really know how to achieve it)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Do you use annotations for the relation? there you can add an orderby https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_orderby – Vyctorya Oct 05 '18 at 12:57
  • @Vyctorya I use annotation but I can use it this because the proprety I want to use for orderBy is not in the current entity. It's in the badge entity linked with relation – Dessauges Antoine Oct 05 '18 at 13:19
  • Then you nedd to look at this question https://stackoverflow.com/questions/22828669/ordering-doctrine-collection-based-on-associated-entity-when-it-is-not-possible – Vyctorya Oct 05 '18 at 13:28

0 Answers0