Issue when making migration with Doctrine ManyToMany definition

45 views Asked by At

I am using symfony 6 and I am facing an issue when executing the make:migration command, I want actually to bind 2 entities called Team and User. Below the Team Entity

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

       #[ORM\Column(length: 255, nullable: true)]
       private ?string $name = null;

       #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'teams')]
       #[ORM\JoinTable(name: "team_users")]
       #[ORM\JoinColumn(name: "user_id", referencedColumnName: "uuid")]
       #[ORM\InverseJoinColumn(name: "team_id", referencedColumnName: "id")]
       private Collection $members;

       ...

And below the User entity:

   #[ORM\Entity(repositoryClass: UserRepository::class)]
   class User
   {
   #[ORM\Id]
   #[ORM\GeneratedValue(strategy: 'CUSTOM')]
   #[ORM\Column(type: 'uuid', unique: true)]
   #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
   #[Groups([GRP_USER_READ, SerializationGroups::REST])]
   private $uuid;

   #[Groups([GRP_USER_READ, SerializationGroups::REST])]
   #[ORM\Column(type: 'string', length: 180, unique: true)]
   #[Assert\NotBlank]
   #[Assert\Email]
   private $email;

   #[ORM\Column(type: 'json')]
   #[Groups([GRP_USER_READ, SerializationGroups::REST])]
   private $roles = [];

   #[ORM\Column(type: 'string')]
   private $password;
   
   #[ORM\ManyToMany(targetEntity: Team::class, mappedBy: 'members')]
   private Collection $teams;

   ...

Then, when I clear the cache and launch make:migration, I get directly an exception:

Column name "uuid" referenced for relation from App\Entity\Team towards App\Entity\User does not exist.

After checking everything in the database and the User entity, the field named uuid is the right one.

I'm a bit confused about how php8 attributes work, I took this example from the doctrine documentation here, is there something missing to define that relation properly?

1

There are 1 answers

0
onizukaek On BEST ANSWER

Solved, actually it was a misunderstanding in the Attributes configuration, I moved the owning side in the User entity and left a single reference in the Team entity, so here's what you get in User:

   #[ORM\ManyToMany(targetEntity: Team::class, inversedBy: 'members')]
   #[ORM\JoinTable(name: "user_teams")]
   #[ORM\JoinColumn(name: "user_id", referencedColumnName: "uuid")]
   #[ORM\InverseJoinColumn(name: "team_id", referencedColumnName: "id")]
   private Collection $teams;

And in Team:

   #[ORM\ManyToMany(targetEntity: "User", mappedBy: 'teams')]
   private Collection $members;