无法让表单集合在 symfony 2.6 中工作

can't get form collections to work in symfony 2.6

我是 symfony 的新手,我无法让表单集合类型在我的项目中工作,我想做的是模拟这个项目 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.

知道我做错了什么,我花了很多时间搜索但一无所获

看完后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()