Symfony EasyAdminBundle: Filter entities in assotiation field
Best way is override default controller and apply query builder for form like this.
YML:-
easy_admin:
entities:
Department:
class: YourBundle\Entity\Department
controller: YourBundle\Controller\Admin\Model\DepartmentController
In DepartmentController:-
<?php
namespace YourBundle\Controller\Admin\Model;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController;
class DepartmentController extends AdminController
{
public function createDepartmentEntityFormBuilder($entity, $view)
{
$formBuilder = parent::createEntityFormBuilder($entity, $view);
$user = $this->get('security.token_storage')->getToken()->getUser();
$formBuilder->add('survey', EntityType::class, [
'class' => 'YourBundle\Entity\Survey',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('s')
->where('s.user = :user')
->setParameter('user', $user);
},
]);
return $formBuilder;
}
}