如何在 Symfony easy admin crud 面板中创建下拉列表 select?
How to create dropdown select in Symfony easy admin crud panel?
我是 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;
}
}
请帮帮我!
使用点在 Doctrine 协会中进行搜索。
(例如 'book.id' 或 'author.id')而不是 book_id 或 author_id。
或者在 Book 和 Author 中实现 __toString() 方法。
我是 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;
}
}
请帮帮我!
使用点在 Doctrine 协会中进行搜索。
(例如 'book.id' 或 'author.id')而不是 book_id 或 author_id。
或者在 Book 和 Author 中实现 __toString() 方法。