You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SonataAdminBundle 3.68版本自定义列表字段动作始终返回与ID相同整数问题求助

Hey Jérémy, let's sort out this issue with your SonataAdmin custom action button— I've run into something similar before, so I think I know where to look.

The core problems here are likely incorrect template path handling and a small misconfiguration in your list mapper setup. Here's how to fix it step by step:

  1. Fix your template path format
    Twig doesn't recognize backslashes in paths, and you need to use the correct Twig namespace syntax for your template location. For example:

    • If your template lives in templates/bundles/SonataAdminBundle/CRUD/reset_password.html.twig, update your PHP code to use:
      $listMapper->add('Actions', 'HTML', ['template' => '@SonataAdmin/CRUD/reset_password.html.twig']);
      
    • If you're storing the template in your own app's templates/CRUD/ folder, use @App/CRUD/reset_password.html.twig instead.
  2. Avoid using a non-existent entity field name
    You're using 'Actions' as the field name, but your entity doesn't have an Actions property. Sonata tries to call a getActions() method (which doesn't exist) and falls back to displaying unexpected values like the ID. Instead, use a placeholder name or empty string, and explicitly set the label:

    $listMapper->add('', 'HTML', [
        'template' => '@SonataAdmin/CRUD/reset_password.html.twig',
        'label' => 'Actions'
    ]);
    
  3. Improve your template's link routing
    Your current link uses just object.id, which won't generate a valid Symfony route. Replace it with a dynamic Sonata Admin route to ensure it points to the correct action:

    {% extends '@SonataAdmin/CRUD/base_list_field.html.twig' %}
    {% block field %}
        <a class="btn btn-primary" href="{{ path('admin_' ~ admin.uniqid ~ '_reset_password', {'id': object.id}) }}">Reinit. password</a>
    {% endblock %}
    

    This uses the admin's unique ID to build the correct route, so you don't have to hardcode route names.

  4. Clear your Symfony cache
    Don't forget to clear the cache to make sure all template and configuration changes take effect:

    php bin/console cache:clear
    

After making these adjustments, your "Reset Password" button should display correctly in the list view instead of the ID value.

内容的提问来源于stack exchange,提问作者Jérémy Gaultier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:27:47