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:
In read-only pages (index
and 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();
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'
);
renderAsHtml
By default, the HTML contents of the items displayed in the select lists are escaped to avoid security issues like XSS. If you need to render custom HTML contents and you are certain that they are safe to display "as is", set this option to not escape those contents:
1
yield AssociationField::new('...')->renderAsHtml();
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();
);
setSortProperty
If you sort the index
page results using an association field, by default
those results are sorted using the id
property of the associated entity.
Set this option to sort results using any of the other properties of the
associated entity:
1
yield AssociationField::new('user')->setSortProperty('name');