Symfony EasyAdminBundle: Filter Objects in the Association Area

I have a form with an associativity field type (list of related objects).

What I was trying to achieve is a filter of this list in the form of newAction (create a new object).

For example, the following screen is below:

  • There is a Survey object with a User field.
  • There is a unit with the Survey field (@ORM \ ManyToOne) where the User selects a survey.

You can see two available polls, but I want to display only the first one, because its field value is the same as the current user.

enter image description here

This is confusing because I cannot find the values ​​passed in the Survey field during debugging.

+4
1

- , .

YML: -

easy_admin:
    entities:
        Department:
            class: YourBundle\Entity\Department
            controller: YourBundle\Controller\Admin\Model\DepartmentController

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

Source: https://habr.com/ru/post/1658570/


All Articles