Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 224
FOKO THierry Avatar asked Jun 15 '18 14:06

FOKO THierry


1 Answers

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.

like image 143
FOKO THierry Avatar answered Oct 29 '22 17:10

FOKO THierry