PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to add condition in AssociationField in Symfony EasyAdmin

 May 12, 2022     easyadmin, php, symfony     No comments   

Issue

I'm building book management system. where each books have many chapters.

When I filter the list chapter by book I want to when I create new chapter the association field book hidden or at least filtered by the current filter (book).


Solution

AssociationField has a method called setQueryBuilder which help you modify the query.

In your case, your association field should look like:

AssociationField::new('chapters')
            ->setQueryBuilder(function ($queryBuilder) {
                return $queryBuilder->andWhere(''); // your query

            });

You could also use a repository and call its method but returning a query builder (and not the result) inside setQueryBuilder.



Answered By - Dylan Kas
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing