我是Symfony 6的新手,我有2个实体的问题。
AppEntityMission:
AppEntityMission#idtagmissionassign和AppEntityTag#missionstag的映射不一致。
应用 实体标记:关联AppEntityTag#missionstag是指不存在的反向侧域AppEntityMission#tags。
这是两个实体之间的关系:DB
这是任务实体
<?php
namespace AppEntity;
use AppRepositoryMissionRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineDBALTypesTypes;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: MissionRepository::class)]
#[ORMTable(name: 'mission')]
class Mission
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 255)]
private ?string $titlemission = null;
#[ORMColumn(length: 255)]
private ?string $descriptionmission = null;
#[ORMColumn(type: Types::DATETIME_MUTABLE)]
private ?DateTimeInterface $onsetdate = null;
#[ORMColumn(type: Types::DATETIME_MUTABLE)]
private ?DateTimeInterface $deadline = null;
#[ORMColumn]
private ?int $budgetmission = null;
/*#[ORMColumn(length: 255)]
private ?string $codeapemission = null;*/
#[ORMColumn(length: 255)]
private ?string $prioritymission = null;
#[ORMManyToMany(targetEntity: Tag::class, inversedBy: 'missionstag',cascade: ['persist'])]
#[ORMJoinTable(name:'mission_tag')]
private Collection $idtagmissionassign;
#[ORMManyToOne(inversedBy: 'missions')]
#[ORMJoinColumn(nullable: false)]
private ?User $iduser = null;
#[ORMManyToMany(targetEntity: User::class, inversedBy: 'missionsassign')]
private Collection $idmissionassign;
#[ORMColumn(length: 100)]
private ?string $remote = null;
public function __construct()
{
$this->idtagmissionassign = new ArrayCollection();
$this->idmissionassign = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitlemission(): ?string
{
return $this->titlemission;
}
public function setTitlemission(string $titlemission): self
{
$this->titlemission = $titlemission;
return $this;
}
public function getDescriptionmission(): ?string
{
return $this->descriptionmission;
}
public function setDescriptionmission(string $descriptionmission): self
{
$this->descriptionmission = $descriptionmission;
return $this;
}
public function getOnsetdate(): ?DateTimeInterface
{
return $this->onsetdate;
}
public function setOnsetdate(DateTimeInterface $onsetdate): self
{
$this->onsetdate = $onsetdate;
return $this;
}
public function getDeadline(): ?DateTimeInterface
{
return $this->deadline;
}
public function setDeadline(DateTimeInterface $deadline): self
{
$this->deadline = $deadline;
return $this;
}
public function getBudgetmission(): ?int
{
return $this->budgetmission;
}
public function setBudgetmission(int $budgetmission): self
{
$this->budgetmission = $budgetmission;
return $this;
}
/*public function getCodeapemission(): ?string
{
return $this->codeapemission;
}
public function setCodeapemission(string $codeapemission): self
{
$this->codeapemission = $codeapemission;
return $this;
}*/
public function getPrioritymission(): ?string
{
return $this->prioritymission;
}
public function setPrioritymission(string $prioritymission): self
{
$this->prioritymission = $prioritymission;
return $this;
}
/**
* @return Collection<int, tag>
*/
public function getIdtagmissionassign(): Collection
{
return $this->idtagmissionassign;
}
public function addIdtagmissionassign(tag $idtagmissionassign): self
{
if (!$this->idtagmissionassign->contains($idtagmissionassign)) {
$this->idtagmissionassign->add($idtagmissionassign);
}
return $this;
}
public function removeIdtagmissionassign(tag $idtagmissionassign): self
{
$this->idtagmissionassign->removeElement($idtagmissionassign);
return $this;
}
public function getIduser(): ?user
{
return $this->iduser;
}
public function setIduser(?user $iduser): self
{
$this->iduser = $iduser;
return $this;
}
/**
* @return Collection<int, user>
*/
public function getIdmissionassign(): Collection
{
return $this->idmissionassign;
}
public function addIdmissionassign(user $idmissionassign): self
{
if (!$this->idmissionassign->contains($idmissionassign)) {
$this->idmissionassign->add($idmissionassign);
}
return $this;
}
public function removeIdmissionassign(user $idmissionassign): self
{
$this->idmissionassign->removeElement($idmissionassign);
return $this;
}
public function getRemote(): ?string
{
return $this->remote;
}
public function setRemote(string $remote): self
{
$this->remote = $remote;
return $this;
}
public function __toString(){
return $this->titlemission;
}
}
这是标签实体:
<?php
namespace AppEntity;
use AppRepositoryTagRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
#[ORMEntity(repositoryClass: TagRepository::class)]
class Tag
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 100)]
#[AssertNotBlank]
#[AssertLength(min:2)]
#[AssertValid]
private ?string $nomtag = null;
#[ORMManyToMany(targetEntity: Mission::class, mappedBy: 'tags')]
#[ORMJoinTable(name:'mission_tag')]
private Collection $missionstag;
public function __construct()
{
$this->missionstag = new ArrayCollection();
}
// #[ORMManyToMany(targetEntity: Mission::class, inversedBy:"$idtagmissionassign")]
// private $genusScientists;
public function getId(): ?int
{
return $this->id;
}
public function getNomtag(): ?string
{
return $this->nomtag;
}
public function setNomtag(string $nomtag): self
{
$this->nomtag = $nomtag;
return $this;
}
/**
* @return Collection<int, Mission>
*/
public function getMissions(): Collection
{
return $this->missionstag;
}
public function addMission(Mission $mission): self
{
if (!$this->missionstag->contains($mission)) {
$this->missionstag->add($mission);
$mission->addTag($this);
}
return $this;
}
public function removeMission(Mission $mission): self
{
if ($this->missionstag->removeElement($mission)) {
$mission->removeTag($this);
}
return $this;
}
public function __toString(): string
{
return $this->nomtag;
}
}
你有办法帮我解决这个问题吗?
多谢
改变映射的和反向的,没有效果。我尝试了一些东西,但没有成功。
Tag
的性质missionstag
声称,它是由Missions
的性质"映射的;tags
",但是没有这样的属性,取而代之的是idtagmissionassign
。(旁注:您的属性名称是坏,因为它们看起来像数据库列,但您使用的是ORM。
你要问自己的第一个问题是:你的多对多表需要额外的列吗?如果答案是肯定的,ORMManyToMany
甚至不是正确的选择。如果答案是否定的,那么它是合适的。
适合的情况:应该有以下属性+属性:
#[ORMManyToMany(targetEntity: Tag::class, inversedBy: 'missions', cascade: ['persist'])]
#[ORMJoinTable(name: 'mission_tag')]
private Collection $tags;
和标签应该有以下属性+属性:
#[ORMManyToMany(targetEntity: Mission::class, mappedBy: 'tags', cascade: ['persist'])]
#[ORMJoinTable(name: 'mission_tag')] // <- is this the actual table name?
private Collection $missions;
mappedBy
和inversedBy
必须匹配属性另一个实体的名称。
如果您的mission_tag
表没有mission_id
和tag_id
列,您可能必须通过JoinColumn
和InverseJoinColumn
属性显式地命名这些列。
对于这种情况,它不适合,你必须有一个实际的实体MissionTags与OneToMany on Mission/Tag,两者都引用它,并颠倒了ManyToOnes on MissionTags。但这不是你问的。