php - ArrayCollection 的问题,必须管理传递给选择字段的“Doctrine\Common\Collections\ArrayCollection”类型的实体
问题描述
我想在我的 bdd 中添加一个新标签。使用 symfony,我确实做了:crud。在我的实体标签中,我有一个名为 Language 的列。它是与实体语言的多对一关系。
在我的表单中,我想让用户在两种语言之间进行选择,但我不想让他们选择多种语言。但是当我检查渲染时,我遇到了一个问题:
'必须管理传递给选择字段的“Doctrine\Common\Collections\ArrayCollection”类型的实体。也许您忘记将其保留在实体管理器中?
这是我的代码:
<?php
namespace App\Entity;
use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TagRepository::class)
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Language::class, inversedBy="tag", cascade={"persist","remove"})
*/
private $language;
/**
* @ORM\ManyToMany(targetEntity=Video::class, mappedBy="tags", cascade={"persist","remove"})
*/
private $video;
public function __construct()
{
$this->language= new ArrayCollection();
$this->video = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Language[]
*/
public function getLanguage(): Collection
{
return $this->language;
}
public function addLanguage(Language $language): self
{
if (!$this->language->contains($language)) {
$this->language[] = $language;
$language->setTag($this);
}
return $this;
}
public function removeLanguage(Language $language): self
{
if ($this->language->contains($language)) {
$this->language->removeElement($language);
// set the owning side to null (unless already changed)
if ($language->getTag() === $this) {
$language->setTag(null);
}
}
return $this;
}
/**
* @return Collection|Video[]
*/
public function getVideo(): Collection
{
return $this->video;
}
public function addVideo(Video $video): self
{
if (!$this->video->contains($video)) {
$this->video[] = $video;
}
return $this;
}
public function removeVideo(Video $video): self
{
if ($this->video->contains($video)) {
$this->video->removeElement($video);
}
return $this;
}
}
<?php
namespace App\Entity;
use App\Repository\LanguageRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=LanguageRepository::class)
*/
class Language
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=Tag::class, mappedBy="language", cascade={"persist","remove"})
*/
private $tag;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTag(): ?Tag
{
return $this->tag;
}
public function setTag(?Tag $tag): self
{
$this->tag = $tag;
return $this;
}
}
还有我的表格:
<?php
namespace App\Form;
use App\Entity\Language;
use App\Entity\Tag;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('language', EntityType::class, ['class'=>Language::class,
'choice_label'=>'name',
'multiple'=>false,
'expanded' => true,])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Tag::class,
]);
}
}
如果我将“多个”设置为 true,则没有问题,但用户可以选择 2 种语言,但我想让他们只选择一种语言。谢谢!
解决方案
推荐阅读
- python - 迭代相同长度行文件的每个字符串的索引,同时从各个索引中生成每个值的新字符串
- node.js - 如何知道客户端已连接 Eventhub
- linux - 检查进程是否已经死亡
- javascript - 跟踪已选中的 CheckBox
- java - Hadoop:MapReduce MinMax 结果与原始数据集不同
- php - Wordpress is_user_logged_in() 仅适用于管理员,不适用于其他角色
- service-worker - 我无法使用工作箱进行预缓存
- angular - 如何根据当前组件隐藏元素(在组件内)?(我用的是路由器)
- python-3.x - 在 PySimpleGUI 中以编程方式使应用程序全屏显示
- python - 如何正确理解混淆矩阵和分类报告