我正在使用 symfony 6,在执行 make:migration 命令时遇到问题,我实际上想绑定 2 个名为 Team 和 User 的实体。 团队实体下方
#[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;
...
在用户实体下方:
#[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;
...
然后,当我清除缓存并启动 make:migration 时,我直接得到一个异常:
从 App\Entity\Team 中引用关系的列名称“uuid” App\Entity\User 不存在。
检查数据库和用户实体中的所有内容后,名为 uuid 的字段是正确的。
我对 php8 属性的工作原理有点困惑,我从学说文档here中获取了这个示例,是否缺少正确定义该关系的内容?
已解决,实际上这是属性配置中的误解,我在用户实体中移动了拥有方,并在团队实体中留下了单个引用,所以这就是您在用户中得到的内容:
#[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;
团队中:
#[ORM\ManyToMany(targetEntity: "User", mappedBy: 'teams')]
private Collection $members;