symfony - Symfony ArrayCollection
问题描述
我有 2 个实体:
Category
并Article
具有以下关系:
- 每个
Article
都有一个Category
。 - 每个
Category
可能有很多Article
。
问题:
我想使用此表单添加新类别:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('submit', SubmitType::class, [
'label' => 'Add a new category'
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Category::class,
]);
}
这是我在字段名称中输入字符串值时遇到的错误:
Name Error This value should be of type array|IteratorAggregate.
我从未ArrayCollection
在 symfony 中使用过 / Relation,但我尝试将其更改TextType::class
为CollectionType::class
它给了我一个白色方块,我无法输入任何内容。
在一个更通用的问题中:
如何验证表单中的值而不会出现错误:This value should be of type array|IteratorAggregate.
Category
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Unique(message="Cette catégorie existe déjà")
*/
private $name;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param $name
* @return Collection|null
*/
public function setName($name): ?Collection
{
return $this->name = $name;
}
文章
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $image;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $editedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
*/
private $category;
解决方案
推荐阅读
- python - Python - 如何更改默认的 os.sep?
- c++ - 如何在 SDI 应用程序的视图中插入多个 FormView
- javascript - Ajax 验证和 SyntaxError: JSON Parse error: Unrecognized token '<' on Laravel
- excel - ColA 和 ColC 中的重复值
- c++ - 为什么函数给了我错误的号码?
- linux - gzip -l 返回未压缩文件大小的错误值
- python - 删除/删除 PyQtGraph 中的图例
- android - 多次在收藏列表中添加相同的项目
- c - 如何在 C 代码中使用 `&&` 和 `;` 等 shell 运算符
- amazon-web-services - 如何在 AWS (elasticbeanstalk) 中为我的 SaaS 用户自定义域设置 SSL?