我有一个实体Product
与实体具有多对多关系Category
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Category", inversedBy="stalls")
*/
private $categories;
//...
/**
* @return Collection|Category[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
In the ProductCrudController
类我有以下configureFields
method:
public function configureFields(string $pageName): iterable
{
return [
Field::new('name'),
Field::new('description'),
AssociationField::new('categories'),
];
}
创建/编辑时Product
关系中的一切都按预期工作,但在产品列表中,我看到的是产品拥有的类别数量,而不是显示相关类别。我怎样才能改变这种行为?
在下图中,第一个产品有 1 个类别,列表中的第二个产品有 2 个不同的类别。我希望此处显示类别名称。
作为旁注:Category
类有一个__toString
方法返回类别的名称。
EDIT:
我正在寻找的行为与Tags
下图中的列:
您可以为此制作一个模板,如下所示:
// 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 %}
只需将其添加到字段中即可
// in ProductCrudController
AssociationField::new('categories')->setTemplatePath('admin/field/category.html.twig'),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)