symfonysymfony5easyadmin

How to set the AssociationField choice label when using autocomplete() with EasyAdmin 3.x


Is it possible to use a property of the associated Entity as the choice label, rather than the associated Entity ID when using the autocomplete method on an AssociationField in an EasyAdmin EntityCrudController?

Example:

I have 3 Entities with the following properties:

User
  name

Certification
  name

UserCertification
  User
  Certification

I have the following in my UserCertificationCrudController:

public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('user')
                ->autocomplete(),
            AssociationField::new('memberCertification')
                ->autocomplete(),
            'lastRenewed',
            'expiration',
        ];
    }

See the following screenshot when creating a new UserCertification: The autocomplete value is a reference to the respective Entity ID. How would we replace User# with the name property on the user?

Things I have already tried with no success:


Solution

  • Do you try to mmake the __toString() method of the entity ?