使用SonataAdminBundle进行simple_array处理

时间:2016-10-24 12:16:24

标签: php mysql symfony doctrine-orm sonata-admin

我的实体有一个simple_array类型的属性,存储由用户生成的字符串列表(因此choice不适用)。

实体的相关部分:

/**
 * @var array
 *
 * @ORM\Column(type="simple_array")
 */
private $tags;

我想使用SonataAdminBundle显示,创建和编辑存在标签的实体:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('tags', 'collection');
}

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('tags', 'array');
}

该列表有效,但显示[0 => Tag1, 1 => Tag2]我宁愿显示Tag1, Tag2。创建和编辑根本不起作用,不显示标记的输入字段应该是什么。

要明确:标签不是相关实体,它们只是一个字符串数组!

1 个答案:

答案 0 :(得分:2)

要添加/修改您的代码,我建议使用此常规解决方案How to add an array (customisable) to a symfony2 form (with sonata Admin)?

要根据需要自定义列表模式中的数组值(默认情况下),只需将SonataAdminBundle中的list_array.html.twig模板覆盖为以下内容:

{% extends admin.getTemplate('base_list_field') %}

{% block field %}
    {{ value|join(', ') }}
{% endblock %}