简体   繁体   English

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

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

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 我的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
        ;
    }
}

How could I manage a field like sizes with Sonata Admin Bundle? 如何使用Sonata Admin Bundle管理大小字段? Thanks very much 非常感谢

Although the result is not beautiful. 虽然结果并不美观。 I've solved the problem just adding 'array' type to 'sizes' field on configureShowFields and configureListFields methods. 我已经解决了在configureShowFields和configureListFields方法上将'array'类型添加到'sizes'字段的问题。 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 结果显示大小字段,如[0 => 38] [1 => 40] [2 => 42] [3 => 43] [4 => 45] [5 => 46],我希望它类似于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 Sonata Admin Bundle Forms上提供的类型

You have to specifie that sizes is a collection , add 'sizes', 'collection'. 您必须指定尺寸是一个集合,添加'尺寸','集合'。 This is working line SF2 form. 这是SF2工作线的形式。 Types can be found here, you'll found collections' attributes too: http://symfony.com/fr/doc/current/reference/forms/types.html 类型可以在这里找到,你也可以找到集合的属性: http//symfony.com/fr/doc/current/reference/forms/types.html

The 'collection' tricks is working well for simple entity. “集合”技巧适用于简单的实体。

Exemple for my Tag entity : 我的Tag实体的例子:

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

Then you need a toString method in your entity. 然后,您需要在实体中使用toString方法。

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

Easy billy :) 容易比利:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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