这个问题的延续:
我正在尝试嵌入此处官方文档中描述的表单集合。不幸的是,我遇到了以下异常:
表单的视图数据应该是 Acme\SiteBundle\Entity\BlogPost 类的一个实例,但它是一个 (n) 数组。您可以通过将“data_class”选项设置为 null 或添加将 a(n) 数组转换为 Acme\SiteBundle\Entity\BlogPost 实例的视图转换器来避免此错误。
这很奇怪,因为我相信我遵循了官方文档到“t”:
博客帖子类型:
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'));
}
}
评论类型:
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 中定义的关系:
/**
* @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;
所以,我不确定我哪里出错了。有任何想法吗?