entity-relationshipquery-buildermany-to-oneeasyadminsymfony6

How to Add a field in cascade of 2 many to one on Easyadmin 4 Symfony 6


I read and try a lots of things just to add a field witch is in relation. One Dance have a level (beginner, improver...) and one Level have a Style (Country music, disco...). So for a dance I can get the level and associate style. Dance is MTO with Level, and Level is MTO with Style. It work fine in traditionnel controller and in Dance Index twig I can do

{{ dance.level.style }}

It's work fine.

Impossible for me to do that in EasyAdmin: In Danse Crud Controller

yield AssociationField::new('level');

is naturally working fine but how adding the style name? I'm not familiar with Queribuilder if it's the solution. I read Symfony Documentation easyadmin about unmapped fields but I don't undestand "createIndexQueryBuilder" parameters. If you can help me to progress. Thank's in advance

I don't find examples in stack with Easyadmin 4. And (I'm sorry), documentation is not very clear for me. Example:

class UserCrudController extends AbstractCrudController
{
    // ...

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('fullName'),
            // ...
        ];
    }

    public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
    {
        $queryBuilder = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);

        // if user defined sort is not set
        if (0 === count($searchDto->getSort())) {
            $queryBuilder
                ->addSelect('CONCAT(entity.first_name, \' \', entity.last_name) AS HIDDEN full_name')
                ->addOrderBy('full_name', 'DESC');
        }

        return $queryBuilder;
    }
}

Why we have "entity.first_name" (why entity word and not entityDto...). dump parameters don't give me persuasive results


Solution

  • Easy finally.

    You can choice the field you want to be rendered. Basically add __toString in Entity.

    In my case just add for a many to many relation:

    AssociationField::new('dances')
            ->setFormTypeOption('choice_label','level.style'),