1 follower

Trait yii\db\SchemaBuilderTrait

Implemented byyii\db\Migration
Available since version2.0.6
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/SchemaBuilderTrait.php

SchemaBuilderTrait contains shortcut methods to create instances of yii\db\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(),
]);

Public Methods

Hide inherited methods

Method Description Defined By
bigInteger() Creates a bigint column. yii\db\SchemaBuilderTrait
bigPrimaryKey() Creates a big primary key column. yii\db\SchemaBuilderTrait
binary() Creates a binary column. yii\db\SchemaBuilderTrait
boolean() Creates a boolean column. yii\db\SchemaBuilderTrait
char() Creates a char column. yii\db\SchemaBuilderTrait
date() Creates a date column. yii\db\SchemaBuilderTrait
dateTime() Creates a datetime column. yii\db\SchemaBuilderTrait
decimal() Creates a decimal column. yii\db\SchemaBuilderTrait
double() Creates a double column. yii\db\SchemaBuilderTrait
float() Creates a float column. yii\db\SchemaBuilderTrait
integer() Creates an integer column. yii\db\SchemaBuilderTrait
json() Creates a JSON column. yii\db\SchemaBuilderTrait
money() Creates a money column. yii\db\SchemaBuilderTrait
primaryKey() Creates a primary key column. yii\db\SchemaBuilderTrait
smallInteger() Creates a smallint column. yii\db\SchemaBuilderTrait
string() Creates a string column. yii\db\SchemaBuilderTrait
text() Creates a text column. yii\db\SchemaBuilderTrait
time() Creates a time column. yii\db\SchemaBuilderTrait
timestamp() Creates a timestamp column. yii\db\SchemaBuilderTrait
tinyInteger() Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used. yii\db\SchemaBuilderTrait

Protected Methods

Hide inherited methods

Method Description Defined By
getDb() yii\db\SchemaBuilderTrait

Method Details

Hide inherited methods

bigInteger() public method (available since version 2.0.6)

Creates a bigint column.

public yii\db\ColumnSchemaBuilder bigInteger ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function bigInteger($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGINT, $length);
}

            
bigPrimaryKey() public method (available since version 2.0.6)

Creates a big primary key column.

public yii\db\ColumnSchemaBuilder bigPrimaryKey ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function bigPrimaryKey($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGPK, $length);
}

            
binary() public method (available since version 2.0.6)

Creates a binary column.

public yii\db\ColumnSchemaBuilder binary ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function binary($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BINARY, $length);
}

            
boolean() public method (available since version 2.0.6)

Creates a boolean column.

public yii\db\ColumnSchemaBuilder boolean ( )
return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function boolean()
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN);
}

            
char() public method (available since version 2.0.8)

Creates a char column.

public yii\db\ColumnSchemaBuilder char ( $length null )
$length integer|null

Column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function char($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_CHAR, $length);
}

            
date() public method (available since version 2.0.6)

Creates a date column.

public yii\db\ColumnSchemaBuilder date ( )
return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function date()
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATE);
}

            
dateTime() public method (available since version 2.0.6)

Creates a datetime column.

public yii\db\ColumnSchemaBuilder dateTime ( $precision null )
$precision integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function dateTime($precision = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATETIME, $precision);
}

            
decimal() public method (available since version 2.0.6)

Creates a decimal column.

public yii\db\ColumnSchemaBuilder decimal ( $precision null, $scale null )
$precision integer|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 integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function decimal($precision = null, $scale = null)
{
    $length = [];
    if ($precision !== null) {
        $length[] = $precision;
    }
    if ($scale !== null) {
        $length[] = $scale;
    }
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DECIMAL, $length);
}

            
double() public method (available since version 2.0.6)

Creates a double column.

public yii\db\ColumnSchemaBuilder double ( $precision null )
$precision integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function double($precision = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DOUBLE, $precision);
}

            
float() public method (available since version 2.0.6)

Creates a float column.

public yii\db\ColumnSchemaBuilder float ( $precision null )
$precision integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function float($precision = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_FLOAT, $precision);
}

            
getDb() protected abstract method

protected abstract yii\db\Connection getDb ( )
return yii\db\Connection

The database connection to be used for schema building.

                abstract protected function getDb();

            
integer() public method (available since version 2.0.6)

Creates an integer column.

public yii\db\ColumnSchemaBuilder integer ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function integer($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_INTEGER, $length);
}

            
json() public method (available since version 2.0.14)

Creates a JSON column.

public yii\db\ColumnSchemaBuilder json ( )
return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

throws yii\base\Exception

                public function json()
{
    /*
     * TODO Remove in Yii 2.1
     *
     * Disabled due to bug in MySQL extension
     * @link https://bugs.php.net/bug.php?id=70384
     */
    if (version_compare(PHP_VERSION, '5.6', '<') && $this->getDb()->getDriverName() === 'mysql') {
        throw new \yii\base\Exception('JSON column type is not supported in PHP < 5.6');
    }
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_JSON);
}

            
money() public method (available since version 2.0.6)

Creates a money column.

public yii\db\ColumnSchemaBuilder money ( $precision null, $scale null )
$precision integer|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 integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function money($precision = null, $scale = null)
{
    $length = [];
    if ($precision !== null) {
        $length[] = $precision;
    }
    if ($scale !== null) {
        $length[] = $scale;
    }
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_MONEY, $length);
}

            
primaryKey() public method (available since version 2.0.6)

Creates a primary key column.

public yii\db\ColumnSchemaBuilder primaryKey ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function primaryKey($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_PK, $length);
}

            
smallInteger() public method (available since version 2.0.6)

Creates a smallint column.

public yii\db\ColumnSchemaBuilder smallInteger ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function smallInteger($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_SMALLINT, $length);
}

            
string() public method (available since version 2.0.6)

Creates a string column.

public yii\db\ColumnSchemaBuilder string ( $length null )
$length integer|null

Column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function string($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, $length);
}

            
text() public method (available since version 2.0.6)

Creates a text column.

public yii\db\ColumnSchemaBuilder text ( )
return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function text()
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TEXT);
}

            
time() public method (available since version 2.0.6)

Creates a time column.

public yii\db\ColumnSchemaBuilder time ( $precision null )
$precision integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function time($precision = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIME, $precision);
}

            
timestamp() public method (available since version 2.0.6)

Creates a timestamp column.

public yii\db\ColumnSchemaBuilder timestamp ( $precision null )
$precision integer|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.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function timestamp($precision = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIMESTAMP, $precision);
}

            
tinyInteger() public method (available since version 2.0.14)

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

public yii\db\ColumnSchemaBuilder tinyInteger ( $length null )
$length integer|null

Column size or precision definition. This parameter will be ignored if not supported by the DBMS.

return yii\db\ColumnSchemaBuilder

The column instance which can be further customized.

                public function tinyInteger($length = null)
{
    return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TINYINT, $length);
}