简单的用户管理,Symfony 4 Doctrine 错误
Simple Usermanagement, Symfony 4 Doctrine Error
我想在我的网站上创建一个简单的用户管理。我遵循了 Maker-Bundle 的教程,但是如果我 运行 命令 doctrine:schema:update --force
我得到一个错误:
An exception occurred while executing 'CREATE TABLE user (id INT
AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles JSON NOT
NULL, password VARCHAR(255) NOT NULL, UNIQUE INDEX
UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET
utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':
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 'JSON NOT
NULL, password VARCHAR(255) NOT NULL, UNIQUE INDEX
UNIQ_8D93D649E7927C7' at line 1
这是我的用户实体
/**
* @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)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
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;
}
}
我试过几个教程,但每次都会出现这个错误,它似乎与 "roles" 字段的数据类型有关。就像在其他一些教程中一样,我也尝试了 json_array
和 array
,但同样的错误。
我在 Windows10.
上使用带有 MySql 数据库的 XAMPP 服务器
你 运行 MySQL 是哪个版本? JSON 数据类型在 MySQL 5.7.8
中引入
XAMPP MariaDB 默认自带,最新版本 10.1.37,不支持 JSON 数据类型(从 10.2.6 版本开始支持)
您可以卸载 MariaDB,然后从 https://dev.mysql.com/downloads/mysql/ 安装 MySQL
或来自此处 https://downloads.mariadb.org/
的更新版本的 MariaDB
您可能需要检查 mysql 版本并 确保它与 config/packages/doctrine.yaml 中指定的版本相同
Json 类型与 MySql 5.6 及以下版本不兼容,symfony 默认配置为 5.7。
如果你是 运行 5.6 或更低版本,请更新 config/packages/doctrine.yaml,symfony 应该会处理剩下的事情。
我想在我的网站上创建一个简单的用户管理。我遵循了 Maker-Bundle 的教程,但是如果我 运行 命令 doctrine:schema:update --force
我得到一个错误:
An exception occurred while executing 'CREATE TABLE user (id INT
AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles JSON NOT
NULL, password VARCHAR(255) NOT NULL, UNIQUE INDEX
UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET
utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':
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 'JSON NOT
NULL, password VARCHAR(255) NOT NULL, UNIQUE INDEX
UNIQ_8D93D649E7927C7' at line 1
这是我的用户实体
/**
* @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)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
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;
}
}
我试过几个教程,但每次都会出现这个错误,它似乎与 "roles" 字段的数据类型有关。就像在其他一些教程中一样,我也尝试了 json_array
和 array
,但同样的错误。
我在 Windows10.
你 运行 MySQL 是哪个版本? JSON 数据类型在 MySQL 5.7.8
中引入XAMPP MariaDB 默认自带,最新版本 10.1.37,不支持 JSON 数据类型(从 10.2.6 版本开始支持)
您可以卸载 MariaDB,然后从 https://dev.mysql.com/downloads/mysql/ 安装 MySQL 或来自此处 https://downloads.mariadb.org/
的更新版本的 MariaDB您可能需要检查 mysql 版本并 确保它与 config/packages/doctrine.yaml 中指定的版本相同
Json 类型与 MySql 5.6 及以下版本不兼容,symfony 默认配置为 5.7。
如果你是 运行 5.6 或更低版本,请更新 config/packages/doctrine.yaml,symfony 应该会处理剩下的事情。