试图理解 Symfony ManyToMany 错误
Trying to understand Symfony ManyToMany error
我正在努力了解 symfony 和学说,我正在尝试在用户 + 组之间创建多对多关系。我正在使用 "symfony/maker-bundle": "^1.13",
,它允许我通过命令行生成实体。我生成了一个如下所示的用户实体:
/**
* @ApiResource(
* normalizationContext={"groups"={"user:read"}},
* denormalizationContext={"groups"={"user:write"}},
* )
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups({"user:read", "user:write"})
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
* @Groups({"user:write"})
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read"})
*/
private $uuid;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Group", inversedBy="users")
* @Groups({"user:read", "user:write"})
*/
private $groups;
public function __construct()
{
$this->uuid = Uuid:: uuid4()->toString();
$this->groups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getUuid(): ?string
{
return $this->uuid;
}
public function setUuid(string $uuid): self
{
$this->uuid = $uuid;
return $this;
}
/**
* @return Collection|Group[]
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(Group $group): self
{
if ($this->groups->contains($group)) {
$this->groups->removeElement($group);
}
return $this;
}
}
我还生成了您可以在下面看到的组实体:
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\GroupRepository")
*/
class Group
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $uuid;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="groups")
*/
private $users;
public function __construct()
{
$this->uuid = Uuid::uuid4()->toString();
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getUuid(): ?string
{
return $this->uuid;
}
public function setUuid(string $uuid): self
{
$this->uuid = $uuid;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addGroup($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
$user->removeGroup($this);
}
return $this;
}
}
我也在尝试使用 ApiPlatform 为我自动生成端点以创建用户、组等。现在,当我尝试创建新用户时,出现以下错误。
An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2, t0.uuid AS uuid_3 FROM group t0 INNER JOIN user_group ON t0.id = user_group.group_id WHERE user_group.user_id = ?' with params [6]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group t0 INNER JOIN user_group ON t0.id = user_group.group_id WHERE user_group.u' at line 1
我想知道的是:
1)为什么我会收到这个错误?
2) 我该如何解决?
检查我的数据库我可以看到用户记录已创建。我最初的想法是,当 ApiPlatform 尝试 return 新创建的用户对象时,它试图找到关联的组数据,但为什么它会失败并出现 500 错误,而不是只给我一个空白数组或其他东西?
此时数据库中有 0 个组,这是导致我的错误的原因吗?
我不确定,但看起来你正在使用 MariaDB,你是否为它配置了 doctrine?
doctrine:
dbal:
server_version: 'mariadb-10.2.12' # set here your actual DB version
可能会解决您的问题
可能group
是Maria中的保留字(我相信是)
添加一个 @ORM\Table(name="groups")
然后再试一次。
干杯!
我正在努力了解 symfony 和学说,我正在尝试在用户 + 组之间创建多对多关系。我正在使用 "symfony/maker-bundle": "^1.13",
,它允许我通过命令行生成实体。我生成了一个如下所示的用户实体:
/**
* @ApiResource(
* normalizationContext={"groups"={"user:read"}},
* denormalizationContext={"groups"={"user:write"}},
* )
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups({"user:read", "user:write"})
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
* @Groups({"user:write"})
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read"})
*/
private $uuid;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Group", inversedBy="users")
* @Groups({"user:read", "user:write"})
*/
private $groups;
public function __construct()
{
$this->uuid = Uuid:: uuid4()->toString();
$this->groups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getUuid(): ?string
{
return $this->uuid;
}
public function setUuid(string $uuid): self
{
$this->uuid = $uuid;
return $this;
}
/**
* @return Collection|Group[]
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(Group $group): self
{
if ($this->groups->contains($group)) {
$this->groups->removeElement($group);
}
return $this;
}
}
我还生成了您可以在下面看到的组实体:
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\GroupRepository")
*/
class Group
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $uuid;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="groups")
*/
private $users;
public function __construct()
{
$this->uuid = Uuid::uuid4()->toString();
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getUuid(): ?string
{
return $this->uuid;
}
public function setUuid(string $uuid): self
{
$this->uuid = $uuid;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addGroup($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
$user->removeGroup($this);
}
return $this;
}
}
我也在尝试使用 ApiPlatform 为我自动生成端点以创建用户、组等。现在,当我尝试创建新用户时,出现以下错误。
An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2, t0.uuid AS uuid_3 FROM group t0 INNER JOIN user_group ON t0.id = user_group.group_id WHERE user_group.user_id = ?' with params [6]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group t0 INNER JOIN user_group ON t0.id = user_group.group_id WHERE user_group.u' at line 1
我想知道的是: 1)为什么我会收到这个错误? 2) 我该如何解决?
检查我的数据库我可以看到用户记录已创建。我最初的想法是,当 ApiPlatform 尝试 return 新创建的用户对象时,它试图找到关联的组数据,但为什么它会失败并出现 500 错误,而不是只给我一个空白数组或其他东西? 此时数据库中有 0 个组,这是导致我的错误的原因吗?
我不确定,但看起来你正在使用 MariaDB,你是否为它配置了 doctrine?
doctrine:
dbal:
server_version: 'mariadb-10.2.12' # set here your actual DB version
可能会解决您的问题
可能group
是Maria中的保留字(我相信是)
添加一个 @ORM\Table(name="groups")
然后再试一次。
干杯!