9

Is there a way to have a column in a Sonata Admin list that uses a calculated value as the sort by ??

Something like this: ( this does NOT work )

    $mapper->add('stock', null, [
        'sortable' => '(stock.real - stock.inCustomerOrder)',
    ]);
minychillo
  • 395
  • 4
  • 17

2 Answers2

0

Since the 2 values are coming from the same table, what about adding a real column? (and with a Doctrine listener, you could keep it synchronized when entity have changed).

Or, you could override the query generated by Sonata, from the documentation here: https://sonata-project.org/bundles/admin/master/doc/reference/action_list.html#customizing-the-query-used-to-generate-the-list

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
  • You can set a condition / where clause in the custom query. But you cannot sort by a derived or calculated value. – Indivision Dev Jan 15 '18 at 07:01
  • Didnt try, but you should be able to rewrite the query completely, the $query is Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery, a wrapper of the Doctrine QueryBuilder. – Thomas Decaux Jan 15 '18 at 11:31
0

Since sorting is based on database queries, you can not sort by virtual/calculated fields.

Jim Panse
  • 2,220
  • 12
  • 34