【问题标题】:Laravel 3 Schema Table Column CollationLaravel 3 Schema 表列排序规则
【发布时间】:2013-01-13 03:47:16
【问题描述】:

我正在学习 laravel,但我被困在一个简单的过程中。我希望将表格生成为 UTF-8,但 varchar 和文本字段就像 latin-1。

Schema section in guide 根本没有帮助。我找到了this GitHub entry,但它也不起作用(抛出错误)。

我有这样的架构:

<?php

class Create_Authors_Table {

    /**
     * Make changes to the database.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('authors',function($table){
         //$table->charset('utf8'); //does not work
         //$table->collate('utf8_general_ci'); //does not work

         $table->increments('id');
         $table->string('name')->charset('utf8'); //adding ->collate('utf8_general_ci') does not work
         $table->text('bio')->charset('utf8');
         $table->timestamps();
        });
    }

    /**
     * Revert the changes to the database.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('authors');
    }

}

这是 SQL 输出:

CREATE TABLE IF NOT EXISTS `authors` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `bio` text NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

但这正是我需要的:

CREATE TABLE IF NOT EXISTS `authors` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `bio` text NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

我什至把 collation 键放到我的 application/config/database.php 中

'mysql' => array(
    'driver'   => 'mysql',
    'host'     => '',
    'database' => '',
    'username' => '',
    'password' => '',
    'charset'  => 'utf8',
    'collation'=> 'utf8_unicode_ci', //this line was not there out of the box, googling provided me this
    'prefix'   => '',
),

我错过了什么,我该如何解决?

谢谢,

【问题讨论】:

    标签: database database-schema laravel database-migration laravel-3


    【解决方案1】:

    MySQL character setscollation 的支持已添加到 Laravel 4 。 检查这个例子。

    https://github.com/laravel/laravel/pull/897

    【讨论】:

    • 是的,但是当周围没有 Laravel 4 时有人问了这个问题 :)
    【解决方案2】:

    对于 Laravel 3 master 中的单个架构构建,似乎没有实际实现。 (在源代码中找不到任何东西)

    我认为您需要手动执行此操作(尝试使用 raw query

    【讨论】:

      【解决方案3】:

      我还是一样,我想 Laravel 没有办法更改/设置字符集的排序规则。但是您可以在 MySQL 服务器的 my.ini/my.cnf 中更改 charset 的默认排序规则!

      [mysqld]
      #...
      collation-server = utf8_unicode_ci
      #...
      

      【讨论】:

        【解决方案4】:

        刚刚解决了,(使用 mysql 5.5 (5.5.29))和 Linux Mint 13 Mate(基于 Ubuntu 12.04 LTS)

        我在 /etc/mysqld/my.cnf 上添加了这些行

        [client]
        default-character-set = utf8
        
        [mysqld]
        init_connect='SET collation_connection = utf8_unicode_ci'
        character-set-server = utf8
        collation-server = utf8_unicode_ci
        

        这样,SQL 查询在 shell 上正常工作,但在 laravel 或 phpmyadmin 等上,它们没有工作。所以我这样做了:

        我打开 /etc/apache2/conf.d/charset 并删除了 ;从这一行:

        AddDefaultCharset UTF-8
        

        然后我重新启动了apache2和mysql,现在它可以正常工作了。

        稍后编辑:That GitHub Core Hack 就像一种享受。我最终做到了。

        【讨论】:

          【解决方案5】:

          这是我的解决方案

          1.在public $engine;下的laravel/database/schema/table.php中添加public $charset; 2.像这样替换laravel/database/schema/grammars/mysql.php中的函数create

          public function create(Table $table, Fluent $command)
          {
              $columns = implode(', ', $this->columns($table));
          
              // First we will generate the base table creation statement. Other than auto
              // incrementing keys, no indexes will be created during the first creation
              // of the table as they're added in separate commands.
              $sql = 'CREATE TABLE '.$this->wrap($table).' ('.$columns.')';
          
              if ( ! is_null($table->engine))
              {
                  $sql .= ' ENGINE = '.$table->engine;
              }
              if ( ! is_null($table->charset))
              {
                  $sql.="DEFAULT CHARSET=".$table->charset;
              }
              return $sql;
          }
          

          3.现在您可以在迁移 php 中设置任何字符集,例如 $table-&gt;charset='utf8';

          【讨论】:

          • 你知道,我很喜欢你的建议,直到我想起我正在使用作曲家和 Git 存储库。呸。还是)感谢你的建议! :)
          猜你喜欢
          • 2019-11-08
          • 2022-08-08
          • 2021-09-10
          • 2020-09-16
          • 2018-04-23
          • 2011-02-13
          • 2015-10-09
          • 2014-06-15
          • 2015-04-03
          相关资源
          最近更新 更多