SchemaBuilderTrait
SchemaBuilderTrait contains shortcut methods to create instances of [[ColumnSchemaBuilder]].
These can be used in database migrations to define database schema types using a PHP interface. This is useful to define a schema in a DBMS independent way so that the application may run on different DBMS the same way.
For example you may use the following code inside your migration files:
$this->createTable('example_table', [
'id' => $this->primaryKey(),
'name' => $this->string(64)->notNull(),
'type' => $this->integer()->notNull()->defaultValue(10),
'description' => $this->text(),
'rule_name' => $this->string(64),
'data' => $this->text(),
'created_at' => $this->datetime()->notNull(),
'updated_at' => $this->datetime(),
]);
Tags
Table of Contents
Methods
- bigInteger() : ColumnSchemaBuilder
- Creates a bigint column.
- bigPrimaryKey() : ColumnSchemaBuilder
- Creates a big primary key column.
- binary() : ColumnSchemaBuilder
- Creates a binary column.
- boolean() : ColumnSchemaBuilder
- Creates a boolean column.
- char() : ColumnSchemaBuilder
- Creates a char column.
- date() : ColumnSchemaBuilder
- Creates a date column.
- dateTime() : ColumnSchemaBuilder
- Creates a datetime column.
- decimal() : ColumnSchemaBuilder
- Creates a decimal column.
- double() : ColumnSchemaBuilder
- Creates a double column.
- float() : ColumnSchemaBuilder
- Creates a float column.
- integer() : ColumnSchemaBuilder
- Creates an integer column.
- json() : ColumnSchemaBuilder
- Creates a JSON column.
- money() : ColumnSchemaBuilder
- Creates a money column.
- primaryKey() : ColumnSchemaBuilder
- Creates a primary key column.
- smallInteger() : ColumnSchemaBuilder
- Creates a smallint column.
- string() : ColumnSchemaBuilder
- Creates a string column.
- text() : ColumnSchemaBuilder
- Creates a text column.
- time() : ColumnSchemaBuilder
- Creates a time column.
- timestamp() : ColumnSchemaBuilder
- Creates a timestamp column.
- tinyInteger() : ColumnSchemaBuilder
- Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
- getDb() : Connection
Methods
bigInteger()
Creates a bigint column.
public
bigInteger([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
bigPrimaryKey()
Creates a big primary key column.
public
bigPrimaryKey([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
binary()
Creates a binary column.
public
binary([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
boolean()
Creates a boolean column.
public
boolean() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
char()
Creates a char column.
public
char([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
date()
Creates a date column.
public
date() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
dateTime()
Creates a datetime column.
public
dateTime([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. DATETIME(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
decimal()
Creates a decimal column.
public
decimal([int|null $precision = null ][, int|null $scale = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision, which is usually the total number of digits. First parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
- $scale : int|null = null
-
column value scale, which is usually the number of digits after the decimal point. Second parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
double()
Creates a double column.
public
double([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. DOUBLE(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
float()
Creates a float column.
public
float([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. FLOAT(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
integer()
Creates an integer column.
public
integer([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
json()
Creates a JSON column.
public
json() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
money()
Creates a money column.
public
money([int|null $precision = null ][, int|null $scale = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision, which is usually the total number of digits. First parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
- $scale : int|null = null
-
column value scale, which is usually the number of digits after the decimal point. Second parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
primaryKey()
Creates a primary key column.
public
primaryKey([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
smallInteger()
Creates a smallint column.
public
smallInteger([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
string()
Creates a string column.
public
string([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
text()
Creates a text column.
public
text() : ColumnSchemaBuilder
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
time()
Creates a time column.
public
time([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. TIME(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
timestamp()
Creates a timestamp column.
public
timestamp([int|null $precision = null ]) : ColumnSchemaBuilder
Parameters
- $precision : int|null = null
-
column value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision). This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
tinyInteger()
Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
public
tinyInteger([int|null $length = null ]) : ColumnSchemaBuilder
Parameters
- $length : int|null = null
-
column size or precision definition. This parameter will be ignored if not supported by the DBMS.
Tags
Return values
ColumnSchemaBuilder —the column instance which can be further customized.
getDb()
protected
abstract getDb() : Connection
Return values
Connection —the database connection to be used for schema building.