Skip to content
Advertisement

Symfony EasyAdminBundle 3 override the createIndexQueryBuilder()

It said on the EasyAdminBundle doc

For example, the index() action calls to a method named createIndexQueryBuilder() to create the Doctrine query builder used to get the results displayed on the index listing. If you want to customize that listing, itโ€™s better to override the createIndexQueryBuilder() method instead of the entire index() method.

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;
}

Advertisement

Answer

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;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement