如何在Symfony 4中通过学说创建数据库表?

时间:2018-06-28 14:07:51

标签: mysql symfony user-interface doctrine entity

在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]);
    }
}

之后,我想通过终端创建数据库表:

 php bin/console doctrine:migrations:diff
 php bin/console doctrine:migrations:migrate

但是我收到很多错误消息:

  

执行期间迁移20180628135528失败。错误异常   执行'CREATE TAB LE app_users(id INT   AUTO_INCREMENT NOT NULL,用户名VARCHAR(25)NOT NULL,密码   VARCHAR(64)NOT NUL L,通过电子邮件发送VARCHAR(254)NOT NULL,is_active   TINYINT(1)NOT NULL,UNIQUE INDEX UNIQ_C2502824F85E0677(userna me),   唯一索引UNIQ_C2502824E7927C74(电子邮件),主键(id))默认   字符集utf8mb4收集u tf8mb4_unicode_ci ENGINE = InnoDB':

     

SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥   太久了最大密钥长度为767字节

     

在AbstractMySQLDriver.php第125行中:

     

执行'CREATE TABLE app_users(id   INT AUTO_INCREMENT NOT NULL,用户名VARCHAR(25)NOT NULL,   密码VARCHAR(64)NOT NULL,电子邮件VARCHAR(254)NOT NULL,is_active   TINY INT(1)NOT NULL,UNIQUE INDEX UNIQ_C2502824F85E0677(用户名),   唯一索引UNIQ_C2502824E7927C74(电子邮件),主键(id))   默认字符集utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE =   InnoDB':

     

SQLSTATE [42000]:语法错误或访问冲突:1071指定   钥匙太长;最大密钥长度为767字节

     

在PDOConnection.php第109行中:

     

SQLSTATE [42000]:语法错误或访问冲突:1071指定   钥匙太长;最大密钥长度为767字节

     

在PDOConnection.php第107行中:

     

SQLSTATE [42000]:语法错误或访问冲突:1071指定   钥匙太长;最大密钥长度为767字节

1 个答案:

答案 0 :(得分:1)

/**
 * @ORM\Column(type="string", length=254, unique=true)
 */
private $email;

更改为最大长度191(应该足以容纳电子邮件...)

/**
 * @ORM\Column(type="string", length=191, unique=true)
 */
private $email;

或更改您的存储引擎...