Symfony EasyAdminBundle 3 重写 createIndexQuer

EasyAdminBundle 文档上说


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


因此,让我们想象一下,当用户被删除时,我在用户实体中将字段 isDeleted 设置为 true。在索引页面中,我想仅显示 isDeleted = false 的用户。如何为此目的重写 createIndexQueryBuilder() ?


这是方法 createIndexQueryBuilder



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

{

    return $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);

}

我尝试像这样覆盖它,但没有成功



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;

}


狐的传说
浏览 137回答 4
4回答

MMMHUHU

您所需要的只是在 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');    return $response;}

幕布斯7119047

这对我的 EasyAdmin 4.x 有用use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;class CampaignCrudController extends AbstractCrudController{    public function __construct(EntityRepository $entityRepository)    {        $this->entityRepository = $entityRepository;    }    public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): ORMQueryBuilder    {        $response = $this->entityRepository->createQueryBuilder($searchDto, $entityDto, $fields, $filters);        $response->andWhere('entity.user = :user')->setParameter('user', $this->getUser());        return $response;    }

慕雪6442864

EasyAdmin4解决方案注意:关键字(别名)entity很重要use Doctrine\ORM\QueryBuilder;class ArticleCrudController extends AbstractCrudController{     ...     public function configureFields(string $pageName): iterable     {          return [                ...                AssociationField::new('author')->setQueryBuilder(                fn (QueryBuilder $queryBuilder) => $queryBuilder                   ->leftJoin('entity.article', 'a')                   ->andWhere('entity.isActive = 1')                   ->andWhere('a.author is NULL')                  ),                ...            ]      }      ...}

慕码人8056858

对于每个使用接受的答案并通过搜索找到以下 QueryException 的人:QueryException Too many parameters: the query defines 1 parameters and you bound 3EntityCrudController 内的 EasyAdmin v3.x:使用->andWhere()函数使用实体。作为前缀(实际上,使用“实体”,不要替换为您的实体名称)public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder{    $response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);    $response->andWhere('entity.isDeleted = 1');       return $response;}
打开App,查看更多内容
随时随地看视频慕课网APP