在 Sonata Admin 中显示已连接 table 上的行数

Show the number of rows on a connected table in Sonata Admin

我有一个 用户 table,在 Sonata Admin 中有一个列表。

另外,我有一个 file table,其中用户通过 user_id[=26 连接到文件=] 字段.

现在,这是用户列表的配置。到目前为止一切顺利,有效。

// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('username')
        ->addIdentifier('email')
        ->addIdentifier('firstName')
        ->addIdentifier('lastName')
    ;
}

试图在文档中找到它,但我不清楚如何向列表中添加一个字段,其中指示连接的 文件 的数量,甚至更好, 如果用户至少有一个上传的文件,我会在列表的单独字段中有一个关于它的标志。

提前感谢您的帮助!

您必须创建一个自定义模板来显示您想要的信息:

list_files.html.twig

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
    {{ object.files|length }}
</div>
{% endblock %}

在你的列表方法中调用它

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('username')
        ->addIdentifier('email')
        ->addIdentifier('firstName')
        ->addIdentifier('lastName')
        ->add('picture', null, array(
                'template' => 'ApplicationSonataAdminBundle:User:list_files.html.twig'
        ));
    ;
}

您可能需要调整模板路径。

阅读此内容了解更多详情: