我是symfony的新手,我无法在我的项目中使用表单集合类型,我尝试做的是similair这个项目https://github.com/khepin/ProductBundle,所以基本上每个产品都可以有一个或多个标签,所以这里是代码: Product.php
/**
* Khepin\ProductBundle\Entity\Product
*
* @ORM\Table()
* @ORM\Entity
*/
class Product
{
/**
* @ORM\OneToMany(targetEntity="Tag", mappedBy="product", cascade={"persist"})
* @var type
*/
private $tags;
/**
* Add tags
*
* @param Khepin\ProductBundle\Entity\Tag $tags
*/
public function addTags(\Khepin\ProductBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
$tags->setProduct($this);
}
}
Tag.php
/**
* Khepin\ProductBundle\Entity\Tag
*
* @ORM\Table()
* @ORM\Entity
*/
class Tag
{
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="tags")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* @var type
*/
private $product;
}
ProductType.php
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('tags', 'collection', array(
'type' => new TagType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
;
}
public function getName()
{
return 'khepin_productbundle_producttype';
}
public function getDefaultOptions(array $options){
return array('data_class' => 'Khepin\ProductBundle\Entity\Product');
}
}
Product / new.html.twig的枝条模板:
{% extends "::base.html.twig" %}
{% block body %}
<h1>Product creation</h1>
<form action="{{ path('product_create') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<p>
<button type="submit">Create</button>
</p>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('product') }}">
Back to the list
</a>
</li>
<li>
<a href="#" class="jslink">
Add a tag
</a>
</li>
</ul>
{% include "KhepinProductBundle:Product:js.html.twig" %}
{% endblock %}
当ProductController是这样的时候
public function newAction()
{
$product = new Product();
$form = $this->createForm(new ProductType(), $product);
return array(
'product' => $product,
'form' => $form->createView()
);
}
表单呈现时没有标记字段,但是当我向产品添加标记时:
public function newAction()
{
$product= new Product();
$product->addTags(new Tag());
$form = $this->createForm(new ProductType(), $product);
return array(
'product' => $product,
'form' => $form->createView()
);
}
它抛出了这个例子:
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Khepin\ProductBundle\Entity\Tag. You can avoid this error by setting the "data_class" option to "Khepin\ProductBundle\Entity\Tag" or by adding a view transformer that transforms an instance of class Khepin\ProductBundle\Entity\Tag to scalar, array or an instance of \ArrayAccess.
任何关于我做错事的想法,我花了很多时间搜索,但我什么都没发现
答案 0 :(得分:0)
看了this之后 我意识到我错过了这两种方法
在ProductType.php上
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Khepin\ProductBundle\Entity\Product',
));
}
和TagType.php
public function setDefaultOptions (OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Khepin\ProductBundle\Entity\Tag',
));
}
所以基本上我在调用setDefaultOptions()时调用了getDefaultOptions()