Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Bundles
  4. EasyAdminBundle
  5. Fields
  6. EasyAdmin Association Field
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Basic Information
  • Options
    • autocomplete
    • renderAsNativeWidget
    • setCrudController
    • setQueryBuilder
    • renderAsEmbeddedForm

EasyAdmin Association Field

Edit this page

EasyAdmin Association Field

This field displays the contents of a property used to associate Doctrine entities between them (of any type: one-to-one, one-to-many, etc.) In form pages this field is rendered using an advanced autocomplete widget based on TomSelect library.

In form pages (edit and new) it looks like this:

Default style of EasyAdmin association field

In read-only pages (indexand detail) is displayed as a clickable link pointing to the detail action of the related entity.

Basic Information

  • PHP Class: EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField
  • Doctrine DBAL Type used to store this value: integer, guid or any other type that you use to store the ID of the associated entity
  • Symfony Form Type used to render the field: EntityType
  • Rendered as:

    1
    2
    <!-- when loading the page this is transformed into a dynamic field via JavaScript -->
    <select> ... </select>

Options

autocomplete

By default, the field loads all the possible values of the related entity. This creates "out of memory" errors when that entity has hundreds or thousands of values. Use this option to load values dynamically (via Ajax requests) based on user input:

1
yield AssociationField::new('...')->autocomplete();

renderAsNativeWidget

By default, this field is rendered using an advanced JavaScript widget created with the TomSelect library. If you prefer to display a standard <select> element, use this option:

1
yield AssociationField::new('...')->renderAsNativeWidget();

setCrudController

In read-only pages (index and detail) this field is displayed as a clickable link that points to the detail page of the related entity.

By default, EasyAdmin finds the CRUD controller of the related entity automatically. However, if you define more than one CRUD controller for that entity, you'll need to use this option to specify which one to use for the links:

1
yield AssociationField::new('...')->setCrudController(SomeCrudController::class);

setQueryBuilder

By default, EasyAdmin uses a generic database query to find the items of the related entity. Use this option if you need to use a custom query to filter results or to sort them in some specific way.

Similar to the query_builder option of Symfony's EntityType, the value of this option can be a Doctrine\ORM\QueryBuilder object or a callable.

You can use the QueryBuilder objects when the custom query is short and not reused everywhere else in the application:

1
2
3
4
5
6
7
8
9
// get the entity repository somehow...
$someRepository = $this->entityManager->getRepository(SomeEntity::class);

yield AssociationField::new('...')->setQueryBuilder(
    $someRepository->createQueryBuilder('entity')
        ->where('entity.some_property = :some_value')
        ->setParameter('some_value', '...')
        ->orderBy('entity.some_property', 'ASC')
);

Using callables is more convenient when custom queries are complex and are already defined in the entity repository because they are reused in other parts of the application. When using a callable, the QueryBuilder is automatically injected by Symfony as the first argument:

1
2
3
yield AssociationField::new('...')->setQueryBuilder(
    fn (QueryBuilder $queryBuilder) => $queryBuilder->addCriteria('...')
);

Or if you prefer using the repository of the entity:

1
2
3
yield AssociationField::new('...')->setQueryBuilder(
    fn (QueryBuilder $queryBuilder) => $queryBuilder->getEntityManager()->getRepository(Foo::class)->findBySomeCriteria();
);

renderAsEmbeddedForm

By default, to-one associations are rendered in forms as dropdowns where you can select one of the given values. For example, a blog post associated with one author will show a dropdown list to select one of the available authors.

However, sometimes the associated property refers to a value object. For example, a Customer entity related to an Address entity or a Server entity related to an IpAddres entity.

In these cases it doesn't make sense to display a dropdown with all the (potentially millions!) addresses. Instead, it's better to embed the form fields of the related entity (e.g. Address) inside the form of the entity that you are creating or editing (e.g. Customer).

The renderAsEmbeddedForm() option tells EasyAdmin to embed the CRUD form of the associated property instead of showing all its possible values in a dropdown:

1
yield AssociationField::new('...')->renderAsEmbeddedForm();

EasyAdmin looks for the CRUD controller associated to the property automatically. If you need better control about which CRUD controller to use, pass the fully-qualified class name of the controller as the first argument:

1
2
3
4
5
6
7
8
yield AssociationField::new('...')->renderAsEmbeddedForm(CategoryCrudController::class);

// the other optional arguments are the page names passed to the configureFields()
// method of the CRUD controller (this allows you to have a better control of
// the fields displayed on different scenarios)
yield AssociationField::new('...')->renderAsEmbeddedForm(
    CategoryCrudController::class, 'create_category_inside_an_article', 'edit_category_inside_an_article'
);
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    No stress: we've got you covered with our 116 automated quality checks of your code

    No stress: we've got you covered with our 116 automated quality checks of your code

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of dened, a Symfony contributor

    Thanks dened for being a Symfony contributor

    1 commit • 6 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia