我是Symfony的新手。我有一些關系表,如作者、書籍和book_authors。一本書可以有很多作者,一個作者可以有很多書。
而現在,我正試圖用簡單的管理擴展實作crud控制器。
<?php
namespace AppControllerAdmin。
use AppEntityBooks;
使用 EasyCorpBundleEasyAdminBundleConfigCrud。
使用 EasyCorpBundleEasyAdminBundleControllerAbstractCrudController。
使用 EasyCorpBundleEasyAdminBundleFieldIntegerField。
使用 EasyCorpBundleEasyAdminBundleFieldTextField。
class BooksCrudController extends AbstractCrudController
{
public static function getEntityFqcn()。string
{
return Books::class;
}
public function configureFields(string $pageName) 。iterable: $pageName.
{
return [
TextField::new('title')。
IntegerField::new('publish_year')。
TextField::new('isbn')。
IntegerField::new('pages')。
];
}
public function configureCrud(Crud $crud) 。Crud: $crud.
{
return $crud.
->setSearchFields(['publish_year'/span>, 'isbn'/span>, 'title'/span>, 'pages'/span>])
->setPaginatorPageSize(20)。
}
}
而且
<?php
namespace AppControllerAdmin。
使用 AppEntityAuthors。
使用 EasyCorpBundleEasyAdminBundleConfigCrud。
使用 EasyCorpBundleEasyAdminBundleControllerAbstractCrudController。
使用 EasyCorpBundleEasyAdminBundleFieldTextField。
class AuthorsCrudController extends AbstractCrudController
{
public static function getEntityFqcn()。string
{
return Authors::class;
}
public function configureFields title">configureFields(string $pageName) 。iterable: $pageName.
{
return [
TextField::new('name')。
TextField::new('first_name')。
TextField::new('last_name')。
];
}
public function configureCrud(Crud $crud) 。Crud: $crud.
{
return $crud.
->setSearchFields(['name'/span>, 'first_name'/span>, 'last_name'/span>])
->setPaginatorPageSize(20)。
}
但是在BookAuthorsCrudController中,我遇到了一個問題:
。<?php
namespace AppControllerAdmin。
use AppEntityBookAuthors;
使用 EasyCorpBundleEasyAdminBundleConfigCrud。
使用 EasyCorpBundleEasyAdminBundleControllerAbstractCrudController。
使用 EasyCorpBundleEasyAdminBundleFieldAssociationField。
class BookAuthorsCrudController extends AbstractCrudController
{
public static function getEntityFqcn()。string
{
return BookAuthors::class。
}
public function configureFields title">configureFields(string $pageName) 。iterable: $pageName.
{
return [
// ChoiceField::new('author')->setChoices(['Joan' => 1, 'Rowling' => 2]),
//這只給了我按id搜索,而不是按我需要的其他欄位,例如我需要:
///作者按姓名 名 姓,書籍按標題。
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 AppEntity;
use AppRepositoryBookAuthorsRepository;
use DoctrineORMMapping as ORM;
/**。
* @ORMEntity(repositoryClass=BookAuthorsRepository::class)
*/
class BookAuthors
{
/**。
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**。
* @ORMManyToOne(targetEntity=Authors::class, inversedBy="books")
* @ORMJoinColumn(nullable=false)
*/
private $author;
/**。
* @ORMManyToOne(targetEntity=Books::class, inversedBy="author")
* @ORMJoinColumn(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;
}
請幫助我!
uj5u.com熱心網友回復:
使用圓點在Doctrine關聯中進行搜索。
(例如'book.id'或'author.id')而不是book_id或author_id。
或者在Book和Author中實作__toString()方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/310781.html
標籤:
下一篇:對陣列進行排序--最小步驟

