如何在 Symfony 4 中通过学说创建数据库 table?
How can I create a database table via doctrine in Symfony 4?
在 Symfony 中我创建了一个实体:
src/Entity/User.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
}
之后我想通过终端创建数据库table:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
但是我收到很多错误信息:
Migration 20180628135528 failed during Execution. Error An exception
occurred while executing 'CREATE TAB LE app_users (id INT
AUTO_INCREMENT NOT NULL, username VARCHAR(25) NOT NULL, password
VARCHAR(64) NOT NUL L, email VARCHAR(254) NOT NULL, is_active
TINYINT(1) NOT NULL, UNIQUE INDEX UNIQ_C2502824F85E0677 (userna me),
UNIQUE INDEX UNIQ_C2502824E7927C74 (email), PRIMARY KEY(id)) DEFAULT
CHARACTER SET utf8mb4 COLLATE u tf8mb4_unicode_ci ENGINE = InnoDB':
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key
was too long; max key length is 767 bytes
In AbstractMySQLDriver.php line 125:
An exception occurred while executing 'CREATE TABLE app_users (id
INT AUTO_INCREMENT NOT NULL, usern ame VARCHAR(25) NOT NULL,
password VARCHAR(64) NOT NULL, email VARCHAR(254) NOT NULL, is_active
TINY INT(1) NOT NULL, UNIQUE INDEX UNIQ_C2502824F85E0677 (username),
UNIQUE INDEX UNIQ_C2502824E7927C74 ( email), PRIMARY KEY(id))
DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE =
InnoDB':
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified
key was too long; max key length i s 767 bytes
In PDOConnection.php line 109:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified
key was too long; max key length i s 767 bytes
In PDOConnection.php line 107:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified
key was too long; max key length i s 767 bytes
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
更改为最大长度 191(对于电子邮件来说应该足够了...)
/**
* @ORM\Column(type="string", length=191, unique=true)
*/
private $email;
或者改变你的存储引擎...
在 Symfony 中我创建了一个实体:
src/Entity/User.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
}
之后我想通过终端创建数据库table:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
但是我收到很多错误信息:
Migration 20180628135528 failed during Execution. Error An exception occurred while executing 'CREATE TAB LE app_users (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(25) NOT NULL, password VARCHAR(64) NOT NUL L, email VARCHAR(254) NOT NULL, is_active TINYINT(1) NOT NULL, UNIQUE INDEX UNIQ_C2502824F85E0677 (userna me), UNIQUE INDEX UNIQ_C2502824E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE u tf8mb4_unicode_ci ENGINE = InnoDB':
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
In AbstractMySQLDriver.php line 125:
An exception occurred while executing 'CREATE TABLE app_users (id INT AUTO_INCREMENT NOT NULL, usern ame VARCHAR(25) NOT NULL, password VARCHAR(64) NOT NULL, email VARCHAR(254) NOT NULL, is_active TINY INT(1) NOT NULL, UNIQUE INDEX UNIQ_C2502824F85E0677 (username), UNIQUE INDEX UNIQ_C2502824E7927C74 ( email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length i s 767 bytes
In PDOConnection.php line 109:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length i s 767 bytes
In PDOConnection.php line 107:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length i s 767 bytes
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
更改为最大长度 191(对于电子邮件来说应该足够了...)
/**
* @ORM\Column(type="string", length=191, unique=true)
*/
private $email;
或者改变你的存储引擎...