SchemaBuilderTrait
API reference › yii › db › SchemaBuilderTrait
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(),]);Methods
Section titled “Methods”| Method | Description | Defined 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 abstract | SchemaBuilderTrait |
Method details
Section titled “Method details”bigInteger()
Section titled “bigInteger() ”public function bigInteger(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a bigint column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
bigPrimaryKey()
Section titled “bigPrimaryKey() ”public function bigPrimaryKey(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a big primary key column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
binary()
Section titled “binary() ”public function binary(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a binary column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
boolean()
Section titled “boolean() ”public function boolean(): yii\db\ColumnSchemaBuilderCreates a boolean column.
char()
Section titled “char() ”public function char(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a char column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
date()
Section titled “date() ”public function date(): yii\db\ColumnSchemaBuilderCreates a date column.
dateTime()
Section titled “dateTime() ”public function dateTime(int|null $precision = null): yii\db\ColumnSchemaBuilderCreates a datetime column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
decimal()
Section titled “decimal() ”public function decimal(int|null $precision = null, int|null $scale = null): yii\db\ColumnSchemaBuilderCreates a decimal column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
double()
Section titled “double() ”public function double(int|null $precision = null): yii\db\ColumnSchemaBuilderCreates a double column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
float()
Section titled “float() ”public function float(int|null $precision = null): yii\db\ColumnSchemaBuilderCreates a float column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
integer()
Section titled “integer() ”public function integer(int|null $length = null): yii\db\ColumnSchemaBuilderCreates an integer column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
json()
Section titled “json() ”public function json(): yii\db\ColumnSchemaBuilderCreates a JSON column.
money()
Section titled “money() ”public function money(int|null $precision = null, int|null $scale = null): yii\db\ColumnSchemaBuilderCreates a money column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
primaryKey()
Section titled “primaryKey() ”public function primaryKey(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a primary key column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
smallInteger()
Section titled “smallInteger() ”public function smallInteger(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a smallint column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
string()
Section titled “string() ”public function string(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a string column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
text()
Section titled “text() ”public function text(): yii\db\ColumnSchemaBuilderCreates a text column.
time()
Section titled “time() ”public function time(int|null $precision = null): yii\db\ColumnSchemaBuilderCreates a time column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
timestamp()
Section titled “timestamp() ”public function timestamp(int|null $precision = null): yii\db\ColumnSchemaBuilderCreates a timestamp column.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int| | 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. |
Returns
ColumnSchemaBuilder: The column instance which can be further customized.
Since
2.0.6
tinyInteger()
Section titled “tinyInteger() ”public function tinyInteger(int|null $length = null): yii\db\ColumnSchemaBuilderCreates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
Parameters
| Parameter | Type | Description |
|---|---|---|
$length | int| | Column 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
getDb()
Section titled “getDb() ”abstract protected function getDb(): yii\db\ConnectionReturns
Connection: The database connection to be used for schema building.