Customized dropdown in Easy Admin

Posted by
  1. At first need to config config/packages/easy_admin.yaml. The controller and query builder must be in the same Class.
        controller: App\Controller\CustomEasyAdmin
        class: App\Entity\Student 
            - { property: 'name' }
            - { property: 'grade', type: 'entity', type_options: { class: 'App\Entity\Grade', required: true, multiple: false, query_builder: ['App\Controller\CustomEasyAdmin', 'crateGradeSelectBox'] } }

2. Now we will create the controller and method

namespace App\Controller;
use App\Repository\GradeRepository;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EasyAdminFormType;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\HttpFoundation\Request;

class CustomEasyAdmin extends EasyAdminController
    private static $user;

    protected function initialize(Request $request){
      self::$user = $this->getUser();

    public function crateGradeSelectBox(GradeRepository $r): QueryBuilder
        $userId = self::$user->getId();
        return $r->createQueryBuilder('g')
           ->innerJoin('', 's')
           ->innerJoin('s.userId', 'u')
           ->where(' = :user')
           ->setParameter('user', $userId)
           ->orderBy('', 'DESC');


Leave a Reply