createIndexQueryBuilder

createIndexQueryBuilder

本文介绍了Symfony EasyAdminBundle 3 覆盖 createIndexQueryBuilder()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在 EasyAdminBundle 文档上说

例如, index() 操作调用名为 createIndexQueryBuilder() 的方法来创建 Doctrine 查询构建器,用于获取显示在索引列表上的结果.如果您想自定义该列表,最好覆盖 createIndexQueryBuilder() 方法而不是整个 index() 方法.

让我们想象一下,当用户被删除时,我的用户实体中的字段 isDeleted 设置为 true.在索引页面中,我只想显示 isDeleted = false 的用户.为此,如何覆盖 createIndexQueryBuilder()?

这里是 createIndexQueryBuilder 方法

公共函数 createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder{返回 $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);}

我试图像这样覆盖它,但没有用

公共函数 createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder{$response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);$response->where('isDeleted', true);返回 $response;}
解决方案

您只需要在 where 子句中添加 entity. :)

public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder{parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);$response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);$response->where('entity.isDeleted = 1');返回 $response;}

It said on the EasyAdminBundle doc

So let's imagine I have in my user entity the field isDeleted set to true when the user is deleted. In the index page, I want to display only user with isDeleted = false.How to override the createIndexQueryBuilder() for this purpose?

Here is the method createIndexQueryBuilder


public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
    return $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
}

I tried to override it like this but it didn't work


public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
    $response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
    $response->where('isDeleted', true);
    return $response;
}
解决方案

All you need is add entity. in where clause :)

public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
    parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);

    $response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
    $response->where('entity.isDeleted = 1');

    return $response;
}

这篇关于Symfony EasyAdminBundle 3 覆盖 createIndexQueryBuilder()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:10