EasyAdmin 3.X - 如何查看相关实体`toString` 而不是列表中的关联数量?

hos*_*eio 3 symfony easyadmin

我有一个Product与实体有 ManyToMany 关系的实体Category

/**
 * @ORM\ManyToMany(targetEntity="App\Domain\Category", inversedBy="stalls")
 */
private $categories;

//...

/**
 * @return Collection|Category[]
 */
public function getCategories(): Collection
{
    return $this->categories;
}
Run Code Online (Sandbox Code Playgroud)

ProductCrudController课堂上,我有以下configureFields方法:

public function configureFields(string $pageName): iterable
{
    return [
        Field::new('name'),
        Field::new('description'),
        AssociationField::new('categories'),
    ];
}
Run Code Online (Sandbox Code Playgroud)

在创建/编辑关系中的Product一切都按预期工作时,但在产品列表中,而不是显示相关类别,我看到了产品具有的类别数量。我怎样才能改变这种行为?

在下图中,第一个产品有 1 个类别,列表中的第二个产品有 2 个不同的类别。我希望在此处显示类别的名称。

在此处输入图片说明

附带说明:Category类有一个__toString返回类别名称的方法。

编辑:

我正在寻找的行为Tags与下图中的列相同:

在此处输入图片说明

br-*_*dev 9

formatValue您可以使用如下方法格式化该值:

->formatValue(function ($value, $entity) {
                $str = $entity->getCategories()[0];
                for ($i = 1; $i < $entity->getCategories()->count(); $i++) {
                    $str = $str . ", " . $entity->getCategories()[$i];
                }
                return $str;
              })
Run Code Online (Sandbox Code Playgroud)


Fla*_*ash 7

你可以像这样制作一个模板:

// somewhere here templates/admin/field/category.html.twig
{% for category in field.value %}
  {%- set url = ea_url()
    .setController('Path\\To\\Your\\CategoryCrudController')
    .setAction('detail')
    .setEntityId(category.id)
  -%}
  <a href="{{ url }}">
    {{ category.name }}{% if not loop.last %}, {% endif %}
  </a>
{% else %}  
  <span class="badge badge-secondary">None</span>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

并将其添加到字段中

// in ProductCrudController
AssociationField::new('categories')->setTemplatePath('admin/field/category.html.twig'),
Run Code Online (Sandbox Code Playgroud)


小智 5

我的详细信息页面上也遇到了同样的问题。因此,我没有使用模板,而是根据pagename

if (Crud::PAGE_DETAIL === $pageName) {
   $field = ArrayField::new('field')->setLabel('label');
} else {
   $field = AssociationField::new('field')->setLabel('label');
}
Run Code Online (Sandbox Code Playgroud)