Symfony 2嵌入式表示ArrayCollection错误

时间:2022-10-22 18:41:20

I have to create a multilingual table so i choose this schema:

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

Article ( id, name_translation_fk)

Translation ( id )

Translation_Text (id, language, translation_fk, text)

Now i need to add names in different languages for an article that allready exists. Doctrine gives me this error:

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


Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be an array, object given, called in */vendor/doctrine/lib/Doctrine/ORM/UnitOfWork.php on line 416 and defined in */vendor/doctrine-common/lib/Doctrine/Common/Collections/ArrayCollection.php line 46

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


I have no clue what the problem could be since all the entities are well declared.I think the issue is somewhere in the Form Class.

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

I have posted below my entities, forms and views implicated.

我在下面发布了我的实体,表格和观点。

Article

 class Article
 {

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

Translation

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;
 }

The form

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';
    }
}

The controller

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

The form view

表单视图

<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 个解决方案

#1


3  

Since it is a ont-to-many relationship, you should use collection:

由于它是一个多对多的关系,你应该使用集合:

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

instead of:

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

#1


3  

Since it is a ont-to-many relationship, you should use collection:

由于它是一个多对多的关系,你应该使用集合:

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

instead of:

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