【问题标题】:Show aggregate count of large number of entities in Symfony 3.4 Easy Admin bundle在 Symfony 3.4 Easy Admin bundle 中显示大量实体的总计数
【发布时间】:2019-05-17 20:32:16
【问题描述】:

我正在尝试为实体列表视图显示相关实体的计数,但是实体太多,因此它在实体中耗尽了内存(它执行简单的count($this->relatedEntities))。

知道如何/在哪里可以覆盖QueryBuilderListController 以添加聚合的COUNT() 列吗?

我尝试扩展 AdminController 并连接到 findAll() 函数(手动为每个对象添加计数),但这并没有给我一个实体列表,而是一个 Pagerfanta 对象。

【问题讨论】:

  • 另外,您可以检查额外的延迟加载或急切加载,当您只对计数感兴趣时,这两种方法都可以避免对相关实体的额外 sql 查询

标签: symfony symfony2-easyadmin easyadmin


【解决方案1】:

这是我修复它的方法:

覆盖自定义 AdminController 中的 renderTemplate:

protected function renderTemplate($actionName, $templatePath, array $parameters = array())
{
    if ($actionName === 'list' && $this->entity['class'] === ClassA::class) {
        //piggyback on virtual property 'count'
        $parameters['fields']['count']['servicecounts'] = $this->MyEntityRepository->getCounts();
    }

    return $this->render($templatePath, $parameters);
}

easy_admin 配置:

  list:
    fields:
    - { property: 'count', template: 'count.html.twig' }

count.html.twig:

{{ field_options.servicecounts[item.id] }}

getCounts 函数:

public function getCounts()
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb
        ->select('s.id, count(ce.recordId)')
        ->from(ClassA::class, 's')
        ->leftJoin(ClassB::class, 'ce', Join::WITH, 's.id = ce.service')
        ->groupBy('s.id')
    ;

    $results = [];
    foreach ($qb->getQuery()->execute() as $row) {
        $results[$row['id']] = $row[1];
    }

    return $results;

}

【讨论】:

    猜你喜欢
    • 2019-05-19
    • 2021-07-04
    • 1970-01-01
    • 2022-01-06
    • 2023-03-26
    • 2018-05-27
    • 2016-05-31
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多