Home > Software engineering >  How to create dropdown select in Symfony easy admin crud panel?
How to create dropdown select in Symfony easy admin crud panel?

Time:09-17

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

And

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

But in BookAuthorsCrudController I have a problems:

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

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

Help me please!

CodePudding user response:

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.

  • Related