Home > OS >  The mappings App\Entity\xx and App\Entity\are inconsistent with each other
The mappings App\Entity\xx and App\Entity\are inconsistent with each other

Time:12-13

I'm new on Symfony 6 and i've got problem with 2 of my entities.

App\Entity\Mission :
The mappings App\Entity\Mission#idtagmissionassign and App\Entity\Tag#missionstag are inconsistent with each other.

App\Entity\Tag : The association App\Entity\Tag#missionstag refers to the inverse side field App\Entity\Mission#tags which does not exist.

This is the relation betewen the 2 entities : DB

This is Mission entity

<?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;
    }
}

This is Tag entity :

<?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;
    }
}

Do you have an idea to help me to solve this problem ?

Many thanks

Change the mappedby and inversedby with no effect. Trying some stuff follow internet guideline with no success.

CodePudding user response:

BC\InventoryBundle\Entity\ProductRecipe: type: entity table: ProductRecipe repositoryClass: BC\InventoryBundle\Entity\ProductRecipeRepository

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

CodePudding user response:

Tag's property missionstag claims, that it is mapped by Missions's property "tags", but there is no such property, instead you have idtagmissionassign. (Side note: Your property names are bad, because they look like database columns, but you're using an ORM.

The first question you would have to ask yourself: will you need additional columns on your many-to-many table. If the answer is yes, ORM\ManyToMany isn't even the right fit. If the answer is no, it fits.

The case where it fits: Mission should have the following property attributes:

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

and the Tag should have the following property attributes:

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

the mappedBy and inversedBy must match the property name on the other entity, respectively.

If your mission_tag table doesn't have columns mission_id and tag_id, you might have to explicitly name the columns via the JoinColumn and InverseJoinColumn Attibutes.

For the case, where it doesn't fit, you would have to have an actual entity MissionTags with OneToMany on Mission/Tag that both reference it, and reversed ManyToOnes on MissionTags. But that wasn't what you asked.

  • Related