简体   繁体   中英

SonataAdminBundle how to sum values of a column in the list view

I've a Symfony 2.8 project where I'm using SonataAdminBundle for basic CRUD of working hours. In the main list view (defined with configureListFields(ListMapper $listMapper) ) there's a column with hours value that I want to sum and show in the footer of the list's table.

What's the best way to achieve this? How can I access the collection shown from the Twig template in order to iterate over all elements and sum the right values?

How can I access the collection shown from the Twig template in order to iterate over all elements and sum the right values?

If you're suggesting doing that in the template, I think you shouldn't, if you're suggesting doing that in the controller with php, I think you shouldn't either. I think you should get the DQL query corresponding to the list from the pager, clone it and modify it to add a call to the SUM() SQL function, the executing and inject the result in your template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM