Search code examples
phpmysqlsymfonydoctrine-ormsonata-admin

simple_array handling with SonataAdminBundle


My entity has a property from type simple_array, storing a list of strings which are generated by the user (so choice does not apply).

The relevant part from the entity:

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

I would like to use the SonataAdminBundle to show, create and edit the entity with the tags present:

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

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

The list works, but shows [0 => Tag1, 1 => Tag2] where I'd rather show Tag1, Tag2. The create and edit does not work at all, showing nothing where the input field for the tags should be.

To be clear: Tags are not a related entity, they are simply an array of strings!


Solution

  • For add/edit your tags I recommend this general solution How to add an array (customisable) to a symfony2 form (with sonata Admin)?


    For customization of array values (by defaults) in list mode as you need, just overwrites the list_array.html.twig template from SonataAdminBundle, to something like this:

    {% extends admin.getTemplate('base_list_field') %}
    
    {% block field %}
        {{ value|join(', ') }}
    {% endblock %}