我有三个不同的实体 - 时事通讯 - NewsletterOptions - 选项
我需要NewsletterOptions实体,因为我需要该实体中的额外属性,所以manyToMany关系是没有选择的。现在我想创建以下表单:
因此我创建了以下FormTypes:
<?php
class NewsletterType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('newsletterOptions', 'collection', array(
'type' => new NewsletterOptionsType()
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Application\Bundle\Entity\Newsletter'
));
}
}
class NewsletterOptionsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('option', 'entity', array(
'property' => 'name',
'class' => 'Bundle:Options',
'multiple' => true,
'expanded' => true
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Application\Bundle\Entity\NewsletterOptions'
));
}
}
指定了以下实体:
Application\Bundle\Entity\Newsletter:
type: entity
id:
id:
type: smallint
id: true
generator:
strategy: AUTO
options:
unsigned: true
fields:
name:
type: string
length: 255
oneToMany:
newsletterOptions:
targetEntity: NewsletterOptions
mappedBy: newsletter
Application\Bundle\Entity\NewsletterOptions:
type: entity
id:
id:
type: smallint
id: true
generator:
strategy: AUTO
options:
usigned: true
manyToOne:
newsletter:
targetEntity: Newsletter
option:
targetEntity: Options
mappedBy: newsletterOptions
Application\Bundle\Entity\Options:
type: entity
id:
id:
type: smallint
id: true
generator:
strategy: AUTO
options:
usigned: true
fields:
name:
type: string
length: 255
regex:
type: string
length: 255
oneToMany:
newsletterOptions:
targetEntity: NewsletterOptions
mappedBy: option
当我尝试运行脚本时,会导致以下错误。设置data_class没有帮助。怎么解决这个问题?
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Application\Bundle\Entity\NewsletterOptions. You can avoid this error by setting the "data_class" option to "Application\Bundle\Entity\NewsletterOptions" or by adding a view transformer that transforms an instance of class Application\Bundle\Entity\NewsletterOptions to scalar, array or an instance of \ArrayAccess.
答案 0 :(得分:0)
应该是:
$builder->add('option', 'entity', array(
(注意,option
不是options
,因为这是您的字段的调用方式)