简体   繁体   中英

How to show a Entity value of type array on list action in Sonata Admin Bundle?

How Can I display array value on entitty in sonata admin bundle list action? I've tried to do something like this:

            ->add('daysOfWeek', null, array('type' => 'array'))

in configureListFields method but then I get error: "An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"

You need to add a template for special fields, I don't think there is a such thing as "default" way of displaying array fields.

The anwser was to do it like so:

->add('daysOfWeek', 'array', array('template' => 'WshBackendBundle:EventAdmin:list_days_of_week.html.twig'))

The template method is working well but for simple entity you can use the 'collection' type

I have a Post entity with many tags. So the $tags variable in the Post entity is collection of Tag.

Now in your PostAdmin:

protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->addIdentifier('name')
            ...
            ->add('tags', ' collection')
            ...
}

Then you need a toString method in your Tag entity.

function __toString() {
    return $this->getName();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM