Skip to content
Advertisement

How to create dropdown select in Symfony easy admin crud panel?

I am new in Symfony. I have relation tables such as authors, books and book_authors. One book can have many authors and one author can have many books. So, here is my tables:

enter image description here

And now, I am trying to implement crud controller with easy admin extension.

<?php

namespace AppControllerAdmin;

use AppEntityBooks;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldIntegerField;
use EasyCorpBundleEasyAdminBundleFieldTextField;

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);
    }
}

And

<?php

namespace AppControllerAdmin;

use AppEntityAuthors;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldTextField;

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);
    }
}

But in BookAuthorsCrudController I have a problems:

<?php

namespace AppControllerAdmin;

use AppEntityBookAuthors;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldAssociationField;

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);
    }
}

I can’t use book_id and author_id to implement search function. Only book and author as it realised in BookAuthor Entity:

<?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="authors")
     * @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;
    }
}

Help me please!

Advertisement

Answer

Use dots to search in Doctrine associations for the search.

(e.g. ‘book.id’ or ‘author.id’) instead of book_id or author_id.

Or implement __toString() method in Book and Author.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement