【问题标题】:Is it possible add tinyInteger or smallInteger to increments on laravel ORM?是否可以在 laravel ORM 的增量中添加 tinyInteger 或 smallInteger?
【发布时间】:2013-11-11 18:28:21
【问题描述】:

是否可以将该代码或类似代码添加到 laravel\Illuminate\Database\Schema\Blueprint 以用于迁移?

public function incrementsTiny($column)
{
   return $this->unsignedTinyInteger($column, true);
}

public function incrementsSmall($column)
{
   return $this->unsignedSmallInteger($column, true);
}

场景:一些临时表增长不高并且有一些有用的信息,或者只是不超过 100 行的小表,需要一些罕见的更新(添加或只是更改)。但是可以添加到框架中吗?有很多信息是很常见的,但有时有些表格没有很多数据。

因为对于增量只有整数或大整数的选项

【问题讨论】:

  • 这当然是可能的。只需转到源代码,查看其他数据类型是如何创建的并添加您的方法,或许可以通过继承 Blueprint 类。但真的有必要吗?您是否会为这项工作带来重大的、切实的改变?或者在 github 上向 Taylor 建议。
  • 泰勒说这个变化对他来说并不重要。我认为这应该是与 eloquent 一起使用的选项。但我不会和他和其他人讨论为什么需要拥有,我在 github 上 forkwed 项目。我认为 Eloquent 是一个很好的工具,但不像许多其他 ORM 那样完整。我知道 ORM 背后的人想要提供 DB 可移植性,而维持一切正常运行就像地狱一样。我喜欢 laravel,但想要一些细微的调整。我认为人们更改应用程序比更改数据库更多,这就是为什么我更喜欢将数据库优化到特定数据库而不是提供可移植性。
  • 我同意布鲁诺的看法;我要添加到您的解决方案中的唯一一件事是,最好在 vendors 文件夹之外的单独目录中继承 Blueprint 类,这样如果您使用作曲家进行更新,它就不会被覆盖。

标签: php orm laravel laravel-4 eloquent


【解决方案1】:

你可以使用类似的东西:

$table->tinyInteger('id')->unsigned()->autoIncrement();

【讨论】:

    【解决方案2】:

    导航到:laravel/vendor/laravel/framework/src/Illuminate/Database/Schema

    打开:Blueprint.php

    查找:

    public function increments($column)
        {
            return $this->unsignedInteger($column, true);
        }
    

    在此之后添加:

         /**
         * Create a new auto-incrementing tiny integer column on the table.
         *
         * @param  string  $column
         * @return \Illuminate\Support\Fluent
         */
        public function incrementsTinyInteger($column)
        {
            return $this->unsignedTinyInteger($column, true);
        }
    
        /**
         * Create a new auto-incrementing small integer column on the table.
         *
         * @param  string  $column
         * @return \Illuminate\Support\Fluent
         */
        public function incrementsSmallInteger($column)
        {
            return $this->unsignedSmallInteger($column, true);
        }
    
        /**
         * Create a new auto-incrementing medium integer column on the table.
         *
         * @param  string  $column
         * @return \Illuminate\Support\Fluent
         */
        public function incrementsMediumInteger($column)
        {
            return $this->unsignedMediumInteger($column, true);
        }
    

    查找:

    public function unsignedInteger($column, $autoIncrement = false)
        {
            return $this->integer($column, $autoIncrement, true);
        }
    

    在此之后添加:

         /**
         * Create a new unsigned tiny integer column on the table.
         *
         * @param  string  $column
         * @param  bool  $autoIncrement
         * @param  bool  $unsigned
         * @return \Illuminate\Support\Fluent
         */
        public function unsignedTinyInteger($column, $autoIncrement = false)
        {
            return $this->tinyInteger($column, $autoIncrement, true);
        }
    
        /**
         * Create a new unsigned small integer column on the table.
         *
         * @param  string  $column
         * @param  bool  $autoIncrement
         * @param  bool  $unsigned
         * @return \Illuminate\Support\Fluent
         */
        public function unsignedSmallInteger($column, $autoIncrement = false)
        {
            return $this->smallInteger($column, $autoIncrement, true);
        }
    
        /**
         * Create a new unsigned medium integer column on the table.
         *
         * @param  string  $column
         * @param  bool  $autoIncrement
         * @param  bool  $unsigned
         * @return \Illuminate\Support\Fluent
         */
        public function unsignedMediumInteger($column, $autoIncrement = false)
        {
            return $this->mediumInteger($column, $autoIncrement, true);
        }
    

    导航到:laravel/vendor/laravel/framework/src/Illuminate/Database/Schema/语法

    打开:MySqlGrammar.php

    查找:protected $serials = array('bigInteger', 'integer');

    更改为:protected $serials = array('bigInteger', 'integer', 'tinyInteger', 'smallInteger', 'mediumInteger');

    另外,在上面的同一个文件中,找到:

    protected function typeTinyInteger(Fluent $column)
        {
            return 'tinyint(1)';
        }
    

    改为:

    protected function typeTinyInteger(Fluent $column)
        {
            return 'tinyint';
        }
    

    如果有人知道如何扩展此文件并在 laravel 中配置使用,并希望分享操作方法,我将不胜感激。但我不知道如何在扩展这些文件后配置一切,这就是我知道如何在 laravel 中执行此操作的方式。

    【讨论】:

    • 我认为将这些代码添加为插件而不是编辑原始文件会更好。你知道我们该怎么做吗?
    【解决方案3】:
    $table->tinyInteger('id', true, true);
    

    您可以在 Illuminate\Database\Schema\Blueprint.php 中看到 tinyInteger 函数定义:

    /**
     * Create a new tiny integer (1-byte) column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
    }
    

    因此,您只需将第二个和第三个参数设置为 true,您就会得到 unsigned autoIncrement not null 主键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 2013-08-29
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2021-07-18
      相关资源
      最近更新 更多