我想建立一个showAction
。我想用“文本”类型在我的代码分类定制秀场的显示标签。看看我的代码:
public function configureShowFields(ShowMapper $show)
{
$show->with("Day")
->add("name", "text", array("label" => "Name"))
->add("categories", "text", array("label" => "Categories"));
$show->end();
}
这是“类别”属性与getter和setter方法:
/**
* @var Category[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\Category\Category", cascade={"persist"})
* @ORM\JoinTable(name="day_category",
* joinColumns={@ORM\JoinColumn(name="day_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
*/
private $categories;
/**
* @return Category[]
*/
public function getCategories()
{
return $this->categories;
}
/**
* @param Category[] $categories
*/
public function setCategories(array $categories): void
{
$this->categories = $categories;
}
我想自定义文本类型类别标签,但如果我这样做,我得到的错误:
一个例外模板的渲染过程中被抛出(“警告:nl2br()预计参数1为字符串,给定对象”)。
下面是我想要的“类别”的图片:Here is what I want for categories
由于这些类别是多对多的财产,你不能按预期方式显示该文本。索纳塔联系使得这一领域的无序列表。
您不能使用文本类型此字段。如果您需要更改标签,请尝试以下。
public function configureShowFields(ShowMapper $show)
{
$show->with("Day")
->add("name", "text", array("label" => "Name"))
->add("categories", null, array("label" => "Categories"));
$show->end();
}
索纳塔管理员拥有许多-to-many关联与showMapper
使用模板。
你可以稍稍适应这一行:
->add('categories', null, [
'template' => '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig'
])
和相关联的类别将被显示为一个无序列表。
你必须提供在__toString
实体Category
方法了。