In my Symfony Api platform project, I have an entity Community
with a custom method getTotalMembers()
that returns the number of members in the community. However, when I try to order my results, it doesn’t work.
#[ApiFilter(OrderFilter::class,
properties: ['id', 'name', 'totalMembers'],
arguments: ['orderParameterName' => 'order']
)]
/**
* @return int current number of users that are members of this community
*/
#[Groups(['community:read','community:read_list'])]
public function getTotalMembers(): ?int
{
return $this->members->count();
}
Is there a simple way to make it work?
2
Answers
After a lot of struggle I was able to solve partially the issue with custom State processor on the many to many join relationship table. It calls method that adds or removes 1 from regular field that I can use for sorting.
The problem now, however, is that factories do not trigger this class and so entities made with factories aren't counted in, which hinders my ability to test this future.
Consider changing the method to a regular field that recalculates when members change. Your current implementation is a very "hacky" way to approach the subject and might struggle in a production environment.