如何在 Symfony Easy admin crud 面板中创建下拉选择?
Posted
技术标签:
【中文标题】如何在 Symfony Easy admin crud 面板中创建下拉选择?【英文标题】:How to create dropdown select in Symfony easy admin crud panel? 【发布时间】:2021-11-01 06:44:13 【问题描述】:我是 Symfony 的新手。我有关系表,例如作者、书籍和 book_authors。一本书可以有很多作者,一个作者可以有很多本书。 所以,这是我的表:
现在,我正在尝试使用简单的管理扩展来实现 crud 控制器。
<?php
namespace App\Controller\Admin;
use App\Entity\Books;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
class BooksCrudController extends AbstractCrudController
public static function getEntityFqcn(): string
return Books::class;
public function configureFields(string $pageName): iterable
return [
TextField::new('title'),
IntegerField::new('publish_year'),
TextField::new('isbn'),
IntegerField::new('pages'),
];
public function configureCrud(Crud $crud): Crud
return $crud
->setSearchFields(['publish_year', 'isbn', 'title', 'pages'])
->setPaginatorPageSize(20);
和
<?php
namespace App\Controller\Admin;
use App\Entity\Authors;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
class AuthorsCrudController extends AbstractCrudController
public static function getEntityFqcn(): string
return Authors::class;
public function configureFields(string $pageName): iterable
return [
TextField::new('name'),
TextField::new('first_name'),
TextField::new('last_name'),
];
public function configureCrud(Crud $crud): Crud
return $crud
->setSearchFields(['name', 'first_name', 'last_name'])
->setPaginatorPageSize(20);
但是在 BookAuthorsCrudController 中我有一个问题:
<?php
namespace App\Controller\Admin;
use App\Entity\BookAuthors;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
class BookAuthorsCrudController extends AbstractCrudController
public static function getEntityFqcn(): string
return BookAuthors::class;
public function configureFields(string $pageName): iterable
return [
// ChoiceField::new('author')->setChoices(['Joan' => 1, 'Rowling' => 2]),
// this give me only search by id, not by other fields that I need, for example I need:
// authors by name + first_name + last_name and books by title.
AssociationField::new('author')->autocomplete(),
AssociationField::new('book')->autocomplete(),
];
public function configureCrud(Crud $crud): Crud
return $crud
->setSearchFields(['book_id', 'author_id'])
->setPaginatorPageSize(20);
我不能使用 book_id 和 author_id 来实现搜索功能。只有在 BookAuthor 实体中实现的书籍和作者:
<?php
namespace App\Entity;
use App\Repository\BookAuthorsRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=BookAuthorsRepository::class)
*/
class BookAuthors
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Authors::class, inversedBy="books")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
/**
* @ORM\ManyToOne(targetEntity=Books::class, inversedBy="authors")
* @ORM\JoinColumn(nullable=false)
*/
private $book;
public function getId(): ?int
return $this->id;
public function getAuthor(): ?Authors
return $this->author;
public function setAuthor(?Authors $author): self
$this->author = $author;
return $this;
public function getBook(): ?Books
return $this->book;
public function setBook(?Books $book): self
$this->book = $book;
return $this;
请帮帮我!
【问题讨论】:
【参考方案1】:使用点在 Doctrine 关联中进行搜索。
(例如“book.id”或“author.id”)而不是 book_id 或 author_id。
或者在 Book 和 Author 中实现 __toString() 方法。
【讨论】:
我不能在 AssociationFields 中使用它...因为它要求我选择 BookAuthors 实体字段。 或者在Book和Author中实现__toString()方法以上是关于如何在 Symfony Easy admin crud 面板中创建下拉选择?的主要内容,如果未能解决你的问题,请参考以下文章
Symfony 5 - Easy Admin 3:关联实体有这么多数据时,AssociationField 的负载很重
如何在 Sonata Admin(Symfony 3.3、PHP 7)中使用自定义 javascript 为模态窗口扩展模板?