Skip to content
PHP Ecosystem
Explore

SchemaBuilderTrait

API reference › yii › db › SchemaBuilderTrait

traityii\db\SchemaBuilderTrait
Since2.0.6
Sourceframework/db/SchemaBuilderTrait.php
Used by 1 type

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(),
]);
MethodDescriptionDefined by
bigInteger()Creates a bigint column.SchemaBuilderTrait
bigPrimaryKey()Creates a big primary key column.SchemaBuilderTrait
binary()Creates a binary column.SchemaBuilderTrait
boolean()Creates a boolean column.SchemaBuilderTrait
char()Creates a char column.SchemaBuilderTrait
date()Creates a date column.SchemaBuilderTrait
dateTime()Creates a datetime column.SchemaBuilderTrait
decimal()Creates a decimal column.SchemaBuilderTrait
double()Creates a double column.SchemaBuilderTrait
float()Creates a float column.SchemaBuilderTrait
integer()Creates an integer column.SchemaBuilderTrait
json()Creates a JSON column.SchemaBuilderTrait
money()Creates a money column.SchemaBuilderTrait
primaryKey()Creates a primary key column.SchemaBuilderTrait
smallInteger()Creates a smallint column.SchemaBuilderTrait
string()Creates a string column.SchemaBuilderTrait
text()Creates a text column.SchemaBuilderTrait
time()Creates a time column.SchemaBuilderTrait
timestamp()Creates a timestamp column.SchemaBuilderTrait
tinyInteger()Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.SchemaBuilderTrait
getDb() protected abstractSchemaBuilderTrait
public function bigInteger(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a bigint column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function bigPrimaryKey(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a big primary key column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function binary(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a binary column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function boolean(): yii\db\ColumnSchemaBuilder

Creates a boolean column.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function char(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a char column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.8

public function date(): yii\db\ColumnSchemaBuilder

Creates a date column.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function dateTime(int|null $precision = null): yii\db\ColumnSchemaBuilder

Creates a datetime column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn value precision. First parameter passed to the column type, e.g. DATETIME(precision). This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function decimal(int|null $precision = null, int|null $scale = null): yii\db\ColumnSchemaBuilder

Creates a decimal column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn 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.
$scaleint|nullColumn 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.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function double(int|null $precision = null): yii\db\ColumnSchemaBuilder

Creates a double column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn value precision. First parameter passed to the column type, e.g. DOUBLE(precision). This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function float(int|null $precision = null): yii\db\ColumnSchemaBuilder

Creates a float column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn value precision. First parameter passed to the column type, e.g. FLOAT(precision). This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function integer(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates an integer column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function json(): yii\db\ColumnSchemaBuilder

Creates a JSON column.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.14

public function money(int|null $precision = null, int|null $scale = null): yii\db\ColumnSchemaBuilder

Creates a money column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn 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.
$scaleint|nullColumn 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.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function primaryKey(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a primary key column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function smallInteger(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a smallint column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function string(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a string column.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function text(): yii\db\ColumnSchemaBuilder

Creates a text column.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function time(int|null $precision = null): yii\db\ColumnSchemaBuilder

Creates a time column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn value precision. First parameter passed to the column type, e.g. TIME(precision). This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function timestamp(int|null $precision = null): yii\db\ColumnSchemaBuilder

Creates a timestamp column.

Parameters

ParameterTypeDescription
$precisionint|nullColumn value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision). This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.6

public function tinyInteger(int|null $length = null): yii\db\ColumnSchemaBuilder

Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.

Parameters

ParameterTypeDescription
$lengthint|nullColumn size or precision definition. This parameter will be ignored if not supported by the DBMS.

Returns

ColumnSchemaBuilder: The column instance which can be further customized.

Since

2.0.14

abstract protected function getDb(): yii\db\Connection

Returns

Connection: The database connection to be used for schema building.