如何使用Sonata管理包在Symfony项目中列出具有简单数组类型字段的对象


How to list objects with a simple array type field on a Symfony project with the Sonata Admin Bundle

我有一个表示产品的类,其属性映射为表示尺寸集合的简单数组

<?php
namespace Middleware'MainBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * Product
 *
 * @ORM'Table(name="product", uniqueConstraints={...})
 * @ORM'Entity(repositoryClass="...")
 */
class Product {
  /**
    * @var integer
    *
    * @ORM'Column(name="id", type="integer")
    * @ORM'Id
    * @ORM'GeneratedValue(strategy="AUTO")
    */
    private $id;
    // more properties
    // ...   

    /**
     * @ORM'Column(type="simple_array", nullable=true)
     */
    private $sizes;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set sizes
     *
     * @param array $sizes
     * @return Product
     */
    public function setSizes($sizes)
    {
        $this->sizes = $sizes;
        return $this;
    }
    /**
     * Get sizes
     *
     * @return array 
     */
    public function getSizes()
    {
        return $this->sizes;
    }
}

我的ProducAdmin类如下所示

    <?php
namespace Middleware'MainBundle'Admin;
use Sonata'AdminBundle'Admin'Admin;
use Sonata'AdminBundle'Datagrid'DatagridMapper;
use Sonata'AdminBundle'Datagrid'ListMapper;
use Sonata'AdminBundle'Form'FormMapper;
use Sonata'AdminBundle'Show'ShowMapper;
class ProductAdmin extends Admin
{
    /**
     * @param DatagridMapper $datagridMapper
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('id')
            ->add('sizes') // I get a error ContextErrorException: Notice: Array to string conversion
        ;
    }
    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('id')            
            ->add('sizes') // I get a error ContextErrorException: Notice: Array to string conversion
            ->add('_action', 'actions', array(
                'actions' => array(
                    'show' => array(),
                    'edit' => array(),
                    'delete' => array(),
                )
            ))
        ;
    }
    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $sizes = array();
        for ($index = 35; $index <= 48; $index++) {
            $sizes[$index] = $index . ""; 
        }
        $formMapper            
            ->add('sizes', 'choice', array('required' => false, 'expanded' => true, 'multiple' => true, 'choices' => $sizes)) // works fine
        ;
    }
    /**
     * @param ShowMapper $showMapper
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('id')            
            ->add('sizes') // I get a error ContextErrorException: Notice: Array to string conversion
        ;
    }
}

如何使用Sonata Admin Bundle管理像size这样的字段?非常感谢

虽然结果不美观。我已经解决了这个问题,只是在configureShowFields和configureListFields方法上添加"数组"类型到"大小"字段。结果显示大小字段类似于[0 => 38][1 => 40][2 => 42][3 => 43][4 => 45][5 => 46],我希望它类似于38,40,42,43,45,46

我所做的改变:

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('id')                
            ->add('sizes', 'array') // I've just added array type
            ->add('_action', 'actions', array(
                'actions' => array(
                    'show' => array(),
                    'edit' => array(),
                    'delete' => array(),
                )
            ))
        ;
    }
    /**
     * @param ShowMapper $showMapper
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('id')                
            ->add('sizes', 'array') // I've just added array type
        ;
    }
如果有更好的方法我会很感激。由于

Sonata管理包表单中可用的类型

你必须指定size是一个集合,添加'sizes', 'collection'。这是SF2形式的工作线。类型可以在这里找到,你也可以找到集合的属性:http://symfony.com/fr/doc/current/reference/forms/types.html

对于简单的实体来说,"集合"技巧很有效。

标签实体示例:

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

那么你需要一个toString方法在你的实体。

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

放轻松