Symfony2异常时试图嵌入一个表单集合 [英] Symfony2 exception when trying to embed a collection of forms
问题描述
此问题的延续:
我'试图嵌入正式文档 here 。不幸的是,我收到以下异常:
I'm trying to embed a collection of forms as described in the official docs here. Unfortunately, I'm getting the following exception:
表单的视图数据预计将是Acme\SiteBundle \\ Entity\BlogPost,但是是(n)数组。您可以通过将data_class选项设置为null或通过添加将(n)数组转换为Acme\SiteBundle\Entity\BlogPost的实例的视图变换器来避免此错误。
The form's view data is expected to be an instance of class Acme\SiteBundle\Entity\BlogPost, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of Acme\SiteBundle\Entity\BlogPost.
这是奇怪的,因为我相信我遵循官方文档到't':
Which is odd, as I believe I followed the official docs to a 't':
BlogPostType:
BlogPostType:
class BlogPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// static text fields/attrs (title, body, etc)
$builder->add('comments', 'collection', array('type' => new CommentType()));
}
public function getName()
{
return 'blogpost';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => 'Acme\SiteBundle\Entity\BlogPost'));
}
}
CommentType:
CommentType:
class CommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('commentId', 'hidden');
$builder->add('commentBody','text',array('label' => 'Comment:','attr'=>array('size'=>80,'class'=>'form-item-input form-type-texfield')));
}
public function getName()
{
return 'comment';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => 'Acme\SiteBundle\Entity\Comment'));
}
}
BlogPost中定义的关系:
Relationship as defined in BlogPost:
/**
* @var \Acme\SiteBundle\Entity\Comment.php
*
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blogpost",
cascade={"all"},orphanRemoval=true)
* @ORM\OrderBy({"commentId" = "ASC"})
*/
private $comments;
在评论中:
/**
* @var BlogPost
*
* @ORM\ManyToOne(targetEntity="BlogPost", inversedBy="comments")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
* })
*/
private $blogPost;
所以,我不知道我错了什么。任何想法?
So, I'm not sure where I've gone wrong. Any ideas?
推荐答案
事实证明,我是一个白痴(大惊喜),并将表单生成器传递给数组。以前工作的旧代码现在已经打破了。 Whee!
Turns out I was an idiot (big surprise there) and was passing the form builder an array. Old code that used to work was breaking it now. Whee!
这篇关于Symfony2异常时试图嵌入一个表单集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!