在我的一个实体中,我有一个数组属性。我认为Sonata Admin Bundle可以处理它,但它似乎需要一些关注。
我非常确定SONATA_TYPE_COLLECTION
字段类型可以处理,但我没有找到关于如何在configureFormFields()
有谁知道如何配置它?
由于
答案 0 :(得分:0)
您可以使用Sonata CollectionType类,该类能够添加和删除数组中的元素:
use Sonata\AdminBundle\Form\Type\CollectionType;
...
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('group')
->add('roles', CollectionType::class, array(
'allow_add' => true,
'allow_delete' => true,
))
->end()
;
}
答案 1 :(得分:0)
我给你一个我曾经使用过的例子: 实体:
/**
* @ORM\Column(type="array", nullable=true)
*/
private $tablaXY = [];
使用Sonata \ AdminBundle \ Form \ Type \ CollectionType;
->add('tablaXY',CollectionType::class, [
'required' => false,
'by_reference' => false, // Use this because of reasons
'allow_add' => true, // True if you want allow adding new entries to the collection
'allow_delete' => true, // True if you want to allow deleting entries
'prototype' => true, // True if you want to use a custom form type
'entry_type' => TablaType::class, // Form type for the Entity that is being attached to the object
],
[
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
]
)
表格:
class TablaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ejeX', TextType::class,['label' => 'Eje X (texto)',
'required' => true,'attr' => array('class' => 'form-control'),])
->add('ejeY', NumberType::class,['label' => 'Eje Y (Número)',
'required' => true])
;
}
}