Symfony 2嵌入式表示ArrayCollection错误

时间:2011-11-01 19:58:07

标签: php arrays class doctrine symfony

我必须创建一个多语言表,所以我选择这个模式:

Article ( id, name_translation_fk)

Translation ( id )

Translation_Text (id, language, translation_fk, text)

现在我需要为已经存在的文章添加不同语言的名称。 Doctrine给了我这个错误:


捕获致命错误:传递给Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()的参数1必须是一个数组,给定对象,在* / vendor / doctrine / lib / Doctrine / ORM / UnitOfWork.php中调用416并在* / vendor / doctrine-common / lib / Doctrine / Common / Collections / ArrayCollection.php第46行中定义


我不知道问题是什么,因为所有实体都被很好地声明了。我认为这个问题出现在Form Class中。

我已在我的实体,表格和观点中发布了相关内容。

文章

 class Article
 {

  /**
   * @ORM\ManyToOne(targetEntity="Translation", inversedBy="article_name", cascade=  {"persist"})
   * @ORM\JoinColumn(name ="name", referencedColumnName="id")
   */
  protected $name;
 }

翻译

class Translation
{

    /**
     * @ORM\OneToMany(targetEntity="Translation_Text", mappedBy="translation", cascade={"persist"})
     */
     public $translation_text;

    /**
    * @ORM\OneToMany(targetEntity="Article", mappedBy="name", cascade={"persist"})
    */
    protected $article_name;      

    public function __construct()
    {
        $this->translation_text = new ArrayCollection();
        $this->article_name = new ArrayCollection();
    }
 }

Translation_Text

class Translation_Text
{

/**
 * @ORM\ManyToOne(targetEntity="Language", inversedBy="translation_text")
 * @ORM\JoinColumn(name ="language_id", referencedColumnName="id")
 */
protected $language;

/**
 * @ORM\ManyToOne(targetEntity="Translation", inversedBy="translation_text")
 * @ORM\JoinColumn(name ="translation_id", referencedColumnName="id")
 */
protected $translation;
 }

表格

class TranslationTextType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('text','text');                    
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Udo\WebserviceBundle\Entity\Translation_Text',
        );
    }

    public function getName()
    {
        return 'translation_text';
    }

}

class TranslationType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {            
        $builder->add('translation_text',new TranslationTextType());
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Udo\WebserviceBundle\Entity\Translation',
        );
    }

    public function getName()
    {
        return 'translation';
    }

}

class ArticleTranslationForm extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name',new TranslationType());
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Udo\WebserviceBundle\Entity\Article',
        );
    }

    public function getName()
    {
        return 'article';
    }
}

控制器

 $article = $em->getRepository('Udo\WebserviceBundle\Entity\Article')->find($id);
 $form = $this->createForm(new ArticleTranslationForm(),$article);

表单视图

<form action="{{path('article_translate', { 'id': entity.id }) }}" method="post" {{        form_enctype(form) }}>
{{form_row(form.name.translation_text.text)}}
{{form_rest(form)}}
<input type="submit" />
</form>

1 个答案:

答案 0 :(得分:3)

由于它是一对多的关系,因此您应该使用collection

$builder->add('translation_text', 'collection', array('type' => new TranslationTextType()));

而不是:

$builder->add('translation_text',new TranslationTextType());