【问题标题】:How can I create a database table via doctrine in Symfony 4?如何通过 Symfony 4 中的学说创建数据库表?
【发布时间】:2018-06-28 14:07:51
【问题描述】:

在 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,唯一索引 UNIQ_C2502824F85E0677(用户名我), 唯一索引 UNIQ_C2502824E7927C74(电子邮件),主键(id))默认 CHARACTER SET utf8mb4 COLLATE 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,唯一索引 UNIQ_C2502824F85E0677(用户名), 唯一索引 UNIQ_C2502824E7927C74(电子邮件),主键(id)) 默认字符集 utf8mb4 整理 utf8mb4_unicode_ci ENGINE = InnoDB':

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

在 PDOConnection.php 第 109 行:

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

在 PDOConnection.php 第 107 行:

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

【问题讨论】:

  • 你用的是Mysql吗?最大字符串长度为 191,您的电子邮件配置为 254。
  • 它通常与 mysql 无关,但更多与使用的存储引擎(MyISAM / InnoDB)有关

标签: mysql symfony user-interface doctrine entity


【解决方案1】:
/**
 * @ORM\Column(type="string", length=254, unique=true)
 */
private $email;

更改为 191 的最大长度(对于电子邮件来说应该足够了......)

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

或者改变你的存储引擎...

【讨论】:

  • 我什至试过app/console doctrine:cache:clear-metadata app/console doctrine:cache:clear-query app/console doctrine:cache:clear-result
  • 当我删除所有迁移版本时它终于可以工作了
  • 问题来自于新的 Symfony 默认字符集和排序规则,现在是 utf8mb4 而不是 utf8。回到utf8 将解决问题,而无需降低字符串长度。但是 utf8mb4 应该可以解决特殊字符问题,因此请根据您的需要选择最佳选项。
猜你喜欢
  • 2016-11-09
  • 2011-04-20
  • 2018-11-16
  • 2019-07-30
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
相关资源
最近更新 更多