我有一个代表产品的类,该产品的属性映射为代表尺寸集合的简单数组
<?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 管理像大小这样的字段?非常感谢
最佳答案
虽然结果并不美好。我已经解决了这个问题,只是在 configureShowFields 和 configureListFields 方法上将“array”类型添加到“sizes”字段。结果显示大小字段如 [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 Forms 上
关于php - 如何使用 Sonata Admin Bundle 在 Symfony 项目中列出具有简单数组类型字段的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520849/