【问题标题】:How to list objects with a simple array type field on a Symfony project with the Sonata Admin Bundle如何使用 Sonata Admin Bundle 在 Symfony 项目中列出具有简单数组类型字段的对象
【发布时间】:2014-05-07 14:38:01
【问题描述】:

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

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

}

我的 ProductAdmin 类如下

    <?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 管理像大小这样的字段?非常感谢

【问题讨论】:

    标签: php symfony sonata-admin


    【解决方案1】:

    虽然结果并不漂亮。我已经解决了这个问题,只需在 configureShowFields 和 configureListFields 方法的 'sizes' 字段中添加 'array' 类型。结果显示像 [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
            ;
        }
    

    我希望有更好的方法。谢谢

    Types availables 在 Sonata Admin Bundle 表单上

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 但是插入数据的表单可以正常工作。我可以标记一些选项(尺寸)并保存新产品。当我想在数据库中显示存储的产品时会发生异常。感谢您的快速回答:)
      【解决方案3】:

      “收集”技巧适用于简单实体。

      我的标签实体示例:

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

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

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

      简单的比利 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-12
        相关资源
        最近更新 更多