【问题标题】:How to Add a field in cascade of 2 many to one on Easyadmin 4 Symfony 6如何在 Easyadmin 4 Symfony 6 上以 2 多对一的级联方式添加字段
【发布时间】:2023-02-17 16:25:21
【问题描述】:

我阅读并尝试了很多东西,只是为了添加一个领域女巫的关系。 一个舞蹈有一个级别(初学者、改进者……),一个级别有一个风格(乡村音乐、迪斯科……)。所以对于舞蹈,我可以获得水平和关联风格。 Dance 是有 Level 的 MTO,Level 是有 Style 的 MTO。它在 traditionnel 控制器和我能做的 Dance Index twig 中工作正常

{{ dance.level.style }}

它工作正常。

我不可能在 EasyAdmin 中做到这一点:在 Danse Crud Controller 中

yield AssociationField::new('level');

自然工作正常但如何添加样式名称?如果它是解决方案,我不熟悉 Queribuilder。我阅读了关于未映射字段的 Symfony 文档 easyadmin,但我不理解“createIndexQueryBuilder”参数。如果你能帮助我进步。提前致谢

我没有在 Easyadmin 4 的堆栈中找到示例。而且(对不起),文档对我来说不是很清楚。 例子:

class UserCrudController extends AbstractCrudController
{
    // ...

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('fullName'),
            // ...
        ];
    }

    public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
    {
        $queryBuilder = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);

        // if user defined sort is not set
        if (0 === count($searchDto->getSort())) {
            $queryBuilder
                ->addSelect('CONCAT(entity.first_name, \' \', entity.last_name) AS HIDDEN full_name')
                ->addOrderBy('full_name', 'DESC');
        }

        return $queryBuilder;
    }
}

为什么我们有“entity.first_name”(为什么是实体词而不是 entityDto ...)。转储参数没有给我有说服力的结果

【问题讨论】:

    标签: entity-relationship query-builder many-to-one easyadmin symfony6


    【解决方案1】:

    终于轻松了

    您可以选择要呈现的字段。基本上在 Entity 中添加 __toString。

    在我的例子中,只需添加多对多关系:

    AssociationField::new('dances')
            ->setFormTypeOption('choice_label','level.style'),
    

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 2016-04-07
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多