symfony MapApp\Entity\xx与App\Entity\彼此不一致

hjzp0vay  于 2022-12-13  发布在  其他
关注(0)|答案(2)|浏览(138)

我是Symfony 6新手我有两个实体有问题
应用\实体\使命:
应用程序\实体\使命#idtagmissionassign与应用程序\实体\标记#missionstagMap彼此不一致.
应用\实体\标记:关联应用程序\实体\标签#任务标志引用了不存在的反向侧字段应用程序\实体\任务#标签。
这是两个实体之间的关系:DB
这是使命实体

<?php

namespace App\Entity;

use App\Repository\MissionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: MissionRepository::class)]
#[ORM\Table(name: 'mission')]
class Mission
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $titlemission = null;

    #[ORM\Column(length: 255)]
    private ?string $descriptionmission = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $onsetdate = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $deadline = null;

    #[ORM\Column]
    private ?int $budgetmission = null;

    /*#[ORM\Column(length: 255)]
    private ?string $codeapemission = null;*/

    #[ORM\Column(length: 255)]
    private ?string $prioritymission = null;

    #[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'missionstag',cascade: ['persist'])]
    #[ORM\JoinTable(name:'mission_tag')]
    private Collection $idtagmissionassign;

    #[ORM\ManyToOne(inversedBy: 'missions')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $iduser = null;

    #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'missionsassign')]
    private Collection $idmissionassign;

    #[ORM\Column(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 App\Entity;

use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: TagRepository::class)]
class Tag
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 100)]
    #[Assert\NotBlank]
    #[Assert\Length(min:2)]
    #[Assert\Valid]
    private ?string $nomtag = null;

    #[ORM\ManyToMany(targetEntity: Mission::class, mappedBy: 'tags')]
    #[ORM\JoinTable(name:'mission_tag')]
    private Collection $missionstag;

    public function __construct()
    {
        $this->missionstag = new ArrayCollection();
    }

//    #[ORM\ManyToMany(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;
    }
}

你有办法帮我解决这个问题吗?
多谢
改变mappedby和inversedby没有效果。尝试一些东西遵循互联网指南没有成功。

gr8qqesn

gr8qqesn1#

Tag的属性missionstag声称,它被Missions的属性“tags“Map,但是没有这样的属性,相反你有idtagmissionassign。(边注:您的属性名称 * 不正确 *,因为它们看起来像数据库列,但您使用的是ORM。
你要问自己的第一个问题是:您的多对多数据表是否需要额外的数据行。如果答案是肯定的,ORM\ManyToMany甚至不适合。如果答案是否定的,它适合。
适合的情况:使命应具有以下属性+属性:

#[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'missions', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'mission_tag')] 
    private Collection $tags;

Tag应具有以下特性+属性:

#[ORM\ManyToMany(targetEntity: Mission::class, mappedBy: 'tags', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'mission_tag')] // <- is this the actual table name?
    private Collection $missions;

mappedByinversedBy必须分别与另一个实体上的属性名称匹配。
如果您的mission_tag表没有mission_idtag_id列,您可能必须通过JoinColumnInverseJoinColumn属性显式命名这些列。
对于这种情况,如果它不适合,你必须有一个实际的实体MissionTags,在Mission/Tag上有OneToMany,两者都引用它,并在MissionTags上反转ManyToOnes。

lskq00tm

lskq00tm2#

BC\库存捆绑包\实体\产品配方:类型:实体表:产品配方资料档案库类:BC\库存捆绑包\实体\产品配方库

id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    amount: // Previously "ammount"
        type: decimal
        presision: 10
        scale: 2

manyToOne:
    products:
        targetEntity: Product
            // "Products" is named correctly but recipe is singular
            // so for the sake of uniformity 
        inversedBy: recipes
        joinColumn:
            name: product_id
            referencedColumnName: id
    recipes:
        targetEntity: Recipe 
            // Previously "Recipes", incorrect entity name
        inversedBy: products
        joinColumn:
            name: recipe_id
            referencedColumnName: id

相关问题