gpt4 book ai didi

php - Symfony 2 嵌入式表单 ArrayCollection 错误

转载 作者:行者123 更新时间:2023-12-04 06:09:16 24 4
gpt4 key购买 nike

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

Article ( id, name_translation_fk)

Translation ( id )

Translation_Text (id, language, translation_fk, text)

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

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

我不知道问题可能是什么,因为所有实体都已正确声明。我认为问题出在表单类的某个地方。

我在下面发布了我的实体、表格和涉及的 View 。

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

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

表单 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>

最佳答案

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

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

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

关于php - Symfony 2 嵌入式表单 ArrayCollection 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com