是否可以将 tinyInteger 或 smallInteger 添加到 laravel ORM 的增量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19711783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible add tinyInteger or smallInteger to increments on laravel ORM?
提问by Undefined Behavior
Is possible to add that code or something like that to laravel\Illuminate\Database\Schema\Blueprintto use with migrations?
是否可以将该代码或类似的代码添加到laravel\Illuminate\Database\Schema\Blueprint以用于迁移?
public function incrementsTiny($column)
{
return $this->unsignedTinyInteger($column, true);
}
public function incrementsSmall($column)
{
return $this->unsignedSmallInteger($column, true);
}
scenario: some temp table that don't grow high and have some useful information or just small table that do not have more than 100 lines and need some rare update (add or just change). But it is possible to add to the framework? Its common to have a lot information, but sometimes sometables dont have a lot of data.
场景:一些临时表不会变高并且有一些有用的信息,或者只是没有超过 100 行并且需要一些罕见更新(添加或只是更改)的小表。但是可以添加到框架中吗?有很多信息是很常见的,但有时某些表没有很多数据。
Because for increments just have the option for integer or bigInteger
因为对于增量只能选择 integer 或 bigInteger
回答by Wael Showair
You can use something like:
你可以使用类似的东西:
$table->tinyInteger('id')->unsigned()->autoIncrement();
回答by Undefined Behavior
1o
1o
Navigate to: laravel/vendor/laravel/framework/src/Illuminate/Database/Schema
导航到:laravel/vendor/laravel/framework/src/Illuminate/Database/Schema
Open: Blueprint.php
打开:Blueprint.php
Find:
找:
public function increments($column)
{
return $this->unsignedInteger($column, true);
}
Add after this:
在此之后添加:
/**
* 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);
}
Find:
找:
public function unsignedInteger($column, $autoIncrement = false)
{
return $this->integer($column, $autoIncrement, true);
}
Add after this:
在此之后添加:
/**
* 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);
}
2o
2o
Navigate to: laravel/vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars
导航到:laravel/vendor/laravel/framework/src/Illuminate/Database/Schema/ Grammars
Open: MySqlGrammar.php
打开:MySqlGrammar.php
Find: protected $serials = array('bigInteger', 'integer');
找: protected $serials = array('bigInteger', 'integer');
Change to: protected $serials = array('bigInteger', 'integer', 'tinyInteger', 'smallInteger', 'mediumInteger');
改成: protected $serials = array('bigInteger', 'integer', 'tinyInteger', 'smallInteger', 'mediumInteger');
3o
3o
plus, in the same file above, find:
另外,在上面的同一个文件中,找到:
protected function typeTinyInteger(Fluent $column)
{
return 'tinyint(1)';
}
Change to:
改成:
protected function typeTinyInteger(Fluent $column)
{
return 'tinyint';
}
If someone know how to extend this files and config the usage in laravel, and want to share the how-to i will apreciate. But I dont know how to config everthing after extend these files and this is the way i know how to do this in laravel.
如果有人知道如何扩展此文件并在 laravel 中配置用法,并想分享操作方法,我将不胜感激。但我不知道如何在扩展这些文件后配置所有内容,这就是我知道如何在 laravel 中执行此操作的方式。
回答by Ilyich
$table->tinyInteger('id', true, true);
You can see tinyInteger function definition in Illuminate\Database\Schema\Blueprint.php:
你可以在 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'));
}
So you just set the 2nd and 3rd parameters to trueand you get unsigned autoIncrement not null primary key.
因此,您只需将第二个和第三个参数设置为true,您就会得到 unsigned autoIncrement not null 主键。