How to add sort by number of votes in admin?
I have a nominee entity with relation One To Many vote. I need to allow to sort by number of votes for nominees.
I try a solution from here: https://github.com/sonata-project/SonataAdminBundle/issues/1077 and first here: Sonata Admin Bundle: show total count of collection on list view
But I get error message: [Semantical Error] line 0, col 184 near 'v_id_count ASC,': Error: 'v_id_count' is not defined.
Here's the code from NomineeAdmin:
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
if ('list' === $context) {
$parameters = $this->getFilterParameters();
if ('getVotesCount' === $parameters['_sort_by']) {
$rootAlias = $query->getRootAliases()[0];
$query
->addSelect('COUNT(v.id) as v_id_count')
->leftJoin($rootAlias . '. votes', 'v')
->groupBy($rootAlias . '.id')
->orderBy('v_id_count', $parameters['_sort_order']);
}
}
return $query;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
//...
->add(
'getVotesCount',
null,
[
'sortable' => true,
'sort_field_mapping' => ['fieldName' => 'id'],
'sort_parent_association_mappings' => [],
]
);
}