FOKO THierry
FOKO THierry

Reputation: 241

Symfony EasyAdminBundle: Dql Filter entities in assotiation field on List not worrking

I have a form with association field type (list of related entities).

There is Quizz entity with field entity "TypeQuizz" (@ORM\ManyToOne). Impossible to use DQl Filter to Display List Quizz .

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Challenge\TypeQuizz")
 * @ORM\JoinColumn(nullable=false)
 */
private $typeQuizz;

Can not display the quiz list based on the codeTypeQuizz of the TypeQuizz entity

File config EasyAdmin.yaml enter image description here

Error to impossible to access property association TypeQuizz from entity Quizz with DQL Filter

enter image description here

How to apply Dql Filter on property association entity?

Upvotes: 6

Views: 4617

Answers (1)

FOKO THierry
FOKO THierry

Reputation: 241

I overwrite method createListQueryBuilder for support entities association

<?php
namespace App\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;

class QuizzChallengeController extends BaseAdminController
{
    protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
    {
        /* @var EntityManager */
        $em = $this->getDoctrine()->getManagerForClass($this->entity['class']);
        /* @var DoctrineQueryBuilder */
        $queryBuilder = $em->createQueryBuilder()
            ->select('entity')
            ->from($this->entity['class'], 'entity')
            ->leftJoin('entity.typeQuizz','typeQuizz');

        if (!empty($dqlFilter)) {
            $queryBuilder->andWhere($dqlFilter);
        }

        if (null !== $sortField) {
            $queryBuilder->orderBy('entity.'.$sortField, $sortDirection ?: 'DESC');
        }

        return $queryBuilder;
    }
}

I modified the dql_filter in the .yml file, deleted the 'entity' and called the alias directly. enter image description here

This solution works for me.

Upvotes: 8

Related Questions