Symfony 6 / easyadmin 4 / 通过相关的多实体字段在索引视图中搜索
易管理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 %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
thx,setSearchFields 就是我正在寻找的。
src\Controller\Admin\Notepad\NoteCrudController.php
thx, setSearchFields is what I was looking for.
src\Controller\Admin\Notepad\NoteCrudController.php