Use easyadmin_autocomplete in custom unmapped filters

Viewed 604

I'm currently trying to build a back office with symfony 4.4 and easyadmin-bundle 2.3.

I have 2 classes : TravelRequest and Traveler.

TravelRequest

/**
 * @ORM\Entity(repositoryClass=TravelRequestRepository::class)
 */
class TravelRequest
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=20)
     */
    private $uid;

    /**
     * @ORM\Column(type="string", length=10)
     */
    private $flightNo;

    /**
     * @ORM\OneToMany(targetEntity=Traveler::class, mappedBy="travelRequest", orphanRemoval=true, cascade={"persist"})
     */
    private $travelers;
}

Traveler

/**
 * @ORM\Entity(repositoryClass=TravelerRepository::class)
 */
class Traveler
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank()
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $lastName;

    /**
     * @ORM\ManyToOne(targetEntity=TravelRequest::class, inversedBy="travelers")
     * @ORM\JoinColumn(nullable=false)
     */
    private $travelRequest;
}

And within easyadmin, I'm displaying the list of all the travelers, which I would like to filter by the flightNo of the linked TravelRequest, which can contain multiple Traveler.

Actually, I'm using below configuration in easy_admin.yaml to filter a Traveler by flightNo :

- { property: 'flight', type: 'App\Form\Filter\FlightNoFilterType', label: 'Numéro de vol 2', mapped: false }

with FlightNoFilterType :

class FlightNoFilterType extends FilterType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('flightNo', EntityType::class, [
            'class' => TravelRequest::class,
            'label' => "Numéro de vol",
            'expanded'           => false,
            'choice_label' => function ($travelRequest) {
                return $travelRequest->getFlightNo();
            },
            'choice_value' => function (?TravelRequest $travelRequest) {
                return $travelRequest ? $travelRequest->getFlightNo() : '0';
            },
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('q')->groupBy('q.flightNo');
            },
        ]);
    }

    /**
     * @inheritDoc
     */
    public function filter(QueryBuilder $queryBuilder, FormInterface $form, array $metadata)
    {
        $queryBuilder->innerJoin('entity.travelRequest','tr')->andWhere('tr.flightNo = :flightNo')
            ->setParameter('flightNo', $_GET["filters"]["flight"]["flightNo"]);
    }
}

The filter is working great, and is rendered like below :

select simple

But the select is a simple HTML select, and it could become hard to display with more than 50 flights. How would it be possible to have an autocomplete filter like below :

enter image description here

For now I tried this :

- { property: 'flightNo', mapped: false, label: 'N° de vol', type: 'entity', type_options: { value_type_options: { class: 'App\Entity\TravelRequest', choice_label: 'flightNo' } }}

But the flights are not grouped by distinct flight, and the filter is not working, I get the following error :

Notice: Undefined index: dataType

at file vendor/easycorp/easyadmin-bundle/src/Form/Filter/Type/EntityFilterType.php line 50

Thanks in advance for any help.

0 Answers
Related