Symfony 6 / easyadmin 4 / 通过相关的多实体字段在索引视图中搜索

发布于 2025-01-14 22:27:32 字数 2499 浏览 5 评论 0原文

易管理4。 SetTemplatePath 在注释网格上显示玩家的 pid、名字和姓氏。 注意实体与玩家实体有很多关联。我想使用默认的 ea 搜索来按玩家 pid、姓名和姓氏(附有图片)查找注释。

src\Controller\Admin\Notepad\NoteCrudController.php

<?php

namespace App\Controller\Admin\Notepad;

use App\Entity\Notepad\Note;
use App\Entity\Notepad\NoteCategory;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use Doctrine\ORM\EntityManagerInterface;

class NoteCrudController extends AbstractCrudController
{

    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public static function getEntityFqcn(): string
    {
        return Note::class;
    }

    public function configureFields(string $pageName): iterable
    {

        $categories = $this->em->getRepository(NoteCategory::class)->findBy(['isActive' => true]);

        yield DateField::new('incidentDate');
        yield TextField::new('title');
        yield AssociationField::new('player')->setTemplatePath('bundles/EasyAdminBundle/notes-index.html.twig');;
        yield TextEditorField::new('description');
        yield DateField::new('createdAt')->onlyOnIndex();
        yield AssociationField::new('category')->onlyOnForms()->setFormTypeOptions(["choices" => $categories]);
        yield BooleanField::new('isActive')->onlyOnForms();

    }

}
    
    

templates\bundles\EasyAdminBundle\notes-index.html.twig

    {# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
    {# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
    {# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
    {% if ea.crud.currentAction == 'index' %}
        {% for value in field.value %}
            <span class="badge badge-secondary">[{{ value.pid }}] {{ value.name }} {{ value.surname }}</span>
        {% endfor %}
    {% else %}
        <span class="badge badge-secondary">{{ field.formattedValue }}</span>
    {% endif %}

搜索

Easyadmin4. SetTemplatePath to display pid, name and surname of player on notes grid.
Note entity is manytomany related to player entity. I want to use default ea search to find notes in by player pid, name and surname (img attached).

src\Controller\Admin\Notepad\NoteCrudController.php

<?php

namespace App\Controller\Admin\Notepad;

use App\Entity\Notepad\Note;
use App\Entity\Notepad\NoteCategory;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use Doctrine\ORM\EntityManagerInterface;

class NoteCrudController extends AbstractCrudController
{

    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public static function getEntityFqcn(): string
    {
        return Note::class;
    }

    public function configureFields(string $pageName): iterable
    {

        $categories = $this->em->getRepository(NoteCategory::class)->findBy(['isActive' => true]);

        yield DateField::new('incidentDate');
        yield TextField::new('title');
        yield AssociationField::new('player')->setTemplatePath('bundles/EasyAdminBundle/notes-index.html.twig');;
        yield TextEditorField::new('description');
        yield DateField::new('createdAt')->onlyOnIndex();
        yield AssociationField::new('category')->onlyOnForms()->setFormTypeOptions(["choices" => $categories]);
        yield BooleanField::new('isActive')->onlyOnForms();

    }

}
    
    

templates\bundles\EasyAdminBundle\notes-index.html.twig

    {# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
    {# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
    {# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
    {% if ea.crud.currentAction == 'index' %}
        {% for value in field.value %}
            <span class="badge badge-secondary">[{{ value.pid }}] {{ value.name }} {{ value.surname }}</span>
        {% endfor %}
    {% else %}
        <span class="badge badge-secondary">{{ field.formattedValue }}</span>
    {% endif %}

search

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2025-01-21 22:27:32

thx,setSearchFields 就是我正在寻找的。
src\Controller\Admin\Notepad\NoteCrudController.php

    public function configureCrud(Crud $crud): Crud
    {
        return $crud->setSearchFields(['player.pid', 'player.name', 'player.surname']);
    }

thx, setSearchFields is what I was looking for.
src\Controller\Admin\Notepad\NoteCrudController.php

    public function configureCrud(Crud $crud): Crud
    {
        return $crud->setSearchFields(['player.pid', 'player.name', 'player.surname']);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文