EasyAdmin:在列表视图中将整数分金额显示为欧元/美元(如带有“除数”的MoneyType)

时间:2019-07-10 12:22:56

标签: php symfony symfony2-easyadmin easyadmin

将货币值存储为整数(即​​美分)时,是否可以在列表视图中将其显示为欧元/美元?

示例:€900作为90000存储在数据库中。 EasyAdmin将此显示为90,000。我想要的是900(或900,00900.00)。

在EasyAdmin的表单视图中,您可以通过以下方式进行配置:

form:
    fields:
        - { property: 'centAmount', type: 'money', type_options: { divisor: 100, currency: 'EUR' } }

1 个答案:

答案 0 :(得分:0)

列表视图不支持

MoneyType,请参见https://github.com/EasyCorp/EasyAdminBundle/issues/1995

这是https://github.com/EasyCorp/EasyAdminBundle/issues/1995#issuecomment-356717049概述的解决方法:

  1. 使用以下命令创建文件/templates/easy_admin/money.html.twig

    {{ (value/100)|number_format(2, ',', '.') }} € {# you can omit the `number_format()` filter if you don’t want the cents to be shown #}
    

    有关EasyAdmin默认使用的模板,请参见/vendor/easycorp/easyadmin-bundle/src/Resources/views/default/field_integer.html.twig

  2. easy_admin.yaml中激活新模板:

    list:
        fields:
            - {property: 'centAmount', template: 'easy_admin/money.html.twig' }
    

结果:900,00 €

参考:https://symfony.com/doc/current/bundles/EasyAdminBundle/book/list-search-show-configuration.html#rendering-entity-properties-with-custom-templates