If you need to restrict the results displayed in an AssociationField in Easyadmin with a condition on the current entity which you are editing, you can do this:
<?php class ProductCrud extends AbstractCrudController { public function __construct( private readonly ProductRepository $productRepository, private readonly RequestStack $requestStack, ) { } public static function getEntityFqcn(): string { return Product::class; } /** * @return iterable<FieldInterface> */ public function configureFields(string $pageName): iterable { $entityId = $this->requestStack->getCurrentRequest()->attributes->get('entityId'); $currentProduct = null; if (null != $entityId) { $currentProduct = $this->productRepository->find($entityId); } return [ AssociationField::new('defaultPrice') ->setQueryBuilder( fn (QueryBuilder $queryBuilder) => $queryBuilder ->andWhere('entity.product = :product') ->setParameter('product', $currentProduct) ), ]; } }
In this example, I want only displayed prices link to the current product (ManyToOne relation defaultPrice on Product, ManyToOne relation product on Price, OneToMany relation prices on Product).
This is a solution for this issues :
Top comments (0)