drls2738 2014-05-07 14:38
浏览 84
已采纳

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

I've a class that represent a product with a property mapped as simple array that represents a sizes collection

<?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;
    }

}

My ProducAdmin class is like follows

    <?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
        ;
    }
}

How could I manage a field like sizes with Sonata Admin Bundle? Thanks very much

  • 写回答

3条回答 默认 最新

  • dongxi4335 2014-05-07 15:45
    关注

    Although the result is not beautiful. I've solved the problem just adding 'array' type to 'sizes' field on configureShowFields and configureListFields methods. The result shows sizes field like [0 => 38] [1 => 40] [2 => 42] [3 => 43] [4 => 45] [5 => 46] and I would like it as similar to 38, 40, 42, 43, 45, 46

    The change I've made:

        /**
         * @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
            ;
        }
    

    I would appreciate a better way. Thanks

    Types availables on Sonata Admin Bundle Forms

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据