Class yii\db\ColumnSchemaBuilder
| Inheritance | yii\db\ColumnSchemaBuilder » yii\base\BaseObject |
|---|---|
| Implements | yii\base\Configurable |
| Subclasses | yii\db\cubrid\ColumnSchemaBuilder, yii\db\mssql\ColumnSchemaBuilder, yii\db\mysql\ColumnSchemaBuilder, yii\db\oci\ColumnSchemaBuilder, yii\db\sqlite\ColumnSchemaBuilder |
| Available since version | 2.0.6 |
| Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/ColumnSchemaBuilder.php |
ColumnSchemaBuilder helps to define database schema types using a PHP interface.
See yii\db\SchemaBuilderTrait for more detailed description and usage examples.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $categoryMap | array | Mapping of abstract column types (keys) to type categories (values). | yii\db\ColumnSchemaBuilder |
| $comment | string | Comment value of the column. | yii\db\ColumnSchemaBuilder |
| $db | yii\db\Connection | The current database connection. | yii\db\ColumnSchemaBuilder |
| $typeCategoryMap | array | Mapping of abstract column types (keys) to type categories (values). | yii\db\ColumnSchemaBuilder |
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $after | string | The column after which this column will be added. | yii\db\ColumnSchemaBuilder |
| $append | mixed | SQL string to be appended to column schema definition. | yii\db\ColumnSchemaBuilder |
| $check | string | The CHECK constraint for the column. |
yii\db\ColumnSchemaBuilder |
| $default | mixed | Default value of the column. | yii\db\ColumnSchemaBuilder |
| $isFirst | boolean | Whether this column is to be inserted at the beginning of the table. | yii\db\ColumnSchemaBuilder |
| $isNotNull | boolean|null | Whether the column is or not nullable. | yii\db\ColumnSchemaBuilder |
| $isUnique | boolean | Whether the column values should be unique. | yii\db\ColumnSchemaBuilder |
| $isUnsigned | boolean | Whether the column values should be unsigned. | yii\db\ColumnSchemaBuilder |
| $length | integer|string|array | Column size or precision definition. | yii\db\ColumnSchemaBuilder |
| $type | string | The column type definition such as INTEGER, VARCHAR, DATETIME, etc. | yii\db\ColumnSchemaBuilder |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\base\BaseObject |
| __construct() | Create a column schema builder instance giving the type and value precision. | yii\db\ColumnSchemaBuilder |
| __get() | Returns the value of an object property. | yii\base\BaseObject |
| __isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
| __set() | Sets value of an object property. | yii\base\BaseObject |
| __toString() | Builds the full string for the column's schema. | yii\db\ColumnSchemaBuilder |
| __unset() | Sets an object property to null. | yii\base\BaseObject |
| after() | Adds an AFTER constraint to the column. |
yii\db\ColumnSchemaBuilder |
| append() | Specify additional SQL to be appended to column definition. | yii\db\ColumnSchemaBuilder |
| canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
| canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
| check() | Sets a CHECK constraint for the column. |
yii\db\ColumnSchemaBuilder |
| className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
| comment() | Specifies the comment for column. | yii\db\ColumnSchemaBuilder |
| defaultExpression() | Specify the default SQL expression for the column. | yii\db\ColumnSchemaBuilder |
| defaultValue() | Specify the default value for the column. | yii\db\ColumnSchemaBuilder |
| first() | Adds an FIRST constraint to the column. |
yii\db\ColumnSchemaBuilder |
| getCategoryMap() | yii\db\ColumnSchemaBuilder | |
| hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
| hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
| init() | Initializes the object. | yii\base\BaseObject |
| notNull() | Adds a NOT NULL constraint to the column. |
yii\db\ColumnSchemaBuilder |
| null() | Adds a NULL constraint to the column. |
yii\db\ColumnSchemaBuilder |
| setCategoryMap() | yii\db\ColumnSchemaBuilder | |
| unique() | Adds a UNIQUE constraint to the column. |
yii\db\ColumnSchemaBuilder |
| unsigned() | Marks column as unsigned. | yii\db\ColumnSchemaBuilder |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| buildAfterString() | Builds the after constraint for the column. Defaults to unsupported. | yii\db\ColumnSchemaBuilder |
| buildAppendString() | Builds the custom string that's appended to column definition. | yii\db\ColumnSchemaBuilder |
| buildCheckString() | Builds the check constraint for the column. | yii\db\ColumnSchemaBuilder |
| buildCommentString() | Builds the comment specification for the column. | yii\db\ColumnSchemaBuilder |
| buildCompleteString() | Returns the complete column definition from input format. | yii\db\ColumnSchemaBuilder |
| buildDefaultString() | Builds the default value specification for the column. | yii\db\ColumnSchemaBuilder |
| buildDefaultValue() | Return the default value for the column. | yii\db\ColumnSchemaBuilder |
| buildFirstString() | Builds the first constraint for the column. Defaults to unsupported. | yii\db\ColumnSchemaBuilder |
| buildLengthString() | Builds the length/precision part of the column. | yii\db\ColumnSchemaBuilder |
| buildNotNullString() | Builds the not null constraint for the column. | yii\db\ColumnSchemaBuilder |
| buildUniqueString() | Builds the unique constraint for the column. | yii\db\ColumnSchemaBuilder |
| buildUnsignedString() | Builds the unsigned string for column. Defaults to unsupported. | yii\db\ColumnSchemaBuilder |
| getTypeCategory() | Returns the category of the column type. | yii\db\ColumnSchemaBuilder |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| CATEGORY_NUMERIC | 'numeric' | yii\db\ColumnSchemaBuilder | |
| CATEGORY_OTHER | 'other' | yii\db\ColumnSchemaBuilder | |
| CATEGORY_PK | 'pk' | yii\db\ColumnSchemaBuilder | |
| CATEGORY_STRING | 'string' | yii\db\ColumnSchemaBuilder | |
| CATEGORY_TIME | 'time' | yii\db\ColumnSchemaBuilder |
Property Details
The column after which this column will be added.
SQL string to be appended to column schema definition.
Mapping of abstract column types (keys) to type categories (values).
Comment value of the column.
The current database connection. It is used mainly to escape strings safely when building the final column schema string.
Whether this column is to be inserted at the beginning of the table.
Whether the column is or not nullable. If this is true, a NOT NULL constraint will be added.
If this is false, a NULL constraint will be added.
Whether the column values should be unique. If this is true, a UNIQUE constraint will be added.
Whether the column values should be unsigned. If this is true, an UNSIGNED keyword will be added.
Column size or precision definition. This is what goes into the parenthesis after the column type. This can be either a string, an integer or an array. If it is an array, the array values will be joined into a string separated by comma.
The column type definition such as INTEGER, VARCHAR, DATETIME, etc.
Mapping of abstract column types (keys) to type categories (values).
\yii\db\Schema::TYPE_PK => self::CATEGORY_PK,
\yii\db\Schema::TYPE_UPK => self::CATEGORY_PK,
\yii\db\Schema::TYPE_BIGPK => self::CATEGORY_PK,
\yii\db\Schema::TYPE_UBIGPK => self::CATEGORY_PK,
\yii\db\Schema::TYPE_CHAR => self::CATEGORY_STRING,
\yii\db\Schema::TYPE_STRING => self::CATEGORY_STRING,
\yii\db\Schema::TYPE_TEXT => self::CATEGORY_STRING,
\yii\db\Schema::TYPE_TINYINT => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_SMALLINT => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_INTEGER => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_BIGINT => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_FLOAT => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_DOUBLE => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_DECIMAL => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_DATETIME => self::CATEGORY_TIME,
\yii\db\Schema::TYPE_TIMESTAMP => self::CATEGORY_TIME,
\yii\db\Schema::TYPE_TIME => self::CATEGORY_TIME,
\yii\db\Schema::TYPE_DATE => self::CATEGORY_TIME,
\yii\db\Schema::TYPE_BINARY => self::CATEGORY_OTHER,
\yii\db\Schema::TYPE_BOOLEAN => self::CATEGORY_NUMERIC,
\yii\db\Schema::TYPE_MONEY => self::CATEGORY_NUMERIC,
]
Method Details
Defined in: yii\base\BaseObject::__call()
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
| public __call( string $name, array $params ): mixed | ||
| $name | string |
The method name |
| $params | array |
Method parameters |
| return | mixed |
The method return value |
|---|---|---|
| throws | yii\base\UnknownMethodException |
when calling unknown method |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Create a column schema builder instance giving the type and value precision.
| public __construct( string $type, integer|string|array|null $length = null, yii\db\Connection|null $db = null, array $config = [] ): mixed | ||
| $type | string |
Type of the column. See $type. |
| $length | integer|string|array|null |
Length or precision of the column. See $length. |
| $db | yii\db\Connection|null |
The current database connection. See $db. |
| $config | array |
Name-value pairs that will be used to initialize the object properties |
public function __construct($type, $length = null, $db = null, $config = [])
{
$this->type = $type;
$this->length = $length;
$this->db = $db;
parent::__construct($config);
}
Defined in: yii\base\BaseObject::__get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;.
See also __set().
| public __get( string $name ): mixed | ||
| $name | string |
The property name |
| return | mixed |
The property value |
|---|---|---|
| throws | yii\base\UnknownPropertyException |
if the property is not defined |
| throws | yii\base\InvalidCallException |
if the property is write-only |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\BaseObject::__isset()
Checks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property).
Note that if the property is not defined, false will be returned.
| public __isset( string $name ): boolean | ||
| $name | string |
The property name or the event name |
| return | boolean |
Whether the named property is set (not null). |
|---|---|---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;.
See also __get().
| public __set( string $name, mixed $value ): mixed | ||
| $name | string |
The property name or the event name |
| $value | mixed |
The property value |
| throws | yii\base\UnknownPropertyException |
if the property is not defined |
|---|---|---|
| throws | yii\base\InvalidCallException |
if the property is read-only |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Builds the full string for the column's schema.
| public __toString( ): string |
public function __toString()
{
switch ($this->getTypeCategory()) {
case self::CATEGORY_PK:
$format = '{type}{check}{comment}{append}';
break;
default:
$format = '{type}{length}{notnull}{unique}{default}{check}{comment}{append}';
}
return $this->buildCompleteString($format);
}
Defined in: yii\base\BaseObject::__unset()
Sets an object property to null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property).
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
| public __unset( string $name ): mixed | ||
| $name | string |
The property name |
| throws | yii\base\InvalidCallException |
if the property is read only. |
|---|---|---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Adds an AFTER constraint to the column.
Note: MySQL, Oracle and Cubrid support only.
| public after( string $after ): $this | ||
| $after | string |
The column after which $this column will be added. |
public function after($after)
{
$this->after = $after;
return $this;
}
Specify additional SQL to be appended to column definition.
Position modifiers will be appended after column definition in databases that support them.
| public append( string $sql ): $this | ||
| $sql | string |
The SQL string to be appended. |
public function append($sql)
{
$this->append = $sql;
return $this;
}
Builds the after constraint for the column. Defaults to unsupported.
| protected buildAfterString( ): string | ||
| return | string |
A string containing the AFTER constraint. |
|---|---|---|
protected function buildAfterString()
{
return '';
}
Builds the custom string that's appended to column definition.
| protected buildAppendString( ): string | ||
| return | string |
Custom string to append. |
|---|---|---|
protected function buildAppendString()
{
return $this->append !== null ? ' ' . $this->append : '';
}
Builds the check constraint for the column.
| protected buildCheckString( ): string | ||
| return | string |
A string containing the CHECK constraint. |
|---|---|---|
protected function buildCheckString()
{
return $this->check !== null ? " CHECK ({$this->check})" : '';
}
Builds the comment specification for the column.
| protected buildCommentString( ): string | ||
| return | string |
A string containing the COMMENT keyword and the comment itself |
|---|---|---|
protected function buildCommentString()
{
return '';
}
Returns the complete column definition from input format.
| protected buildCompleteString( string $format ): string | ||
| $format | string |
The format of the definition. |
| return | string |
A string containing the complete column definition. |
|---|---|---|
protected function buildCompleteString($format)
{
$placeholderValues = [
'{type}' => $this->type,
'{length}' => $this->buildLengthString(),
'{unsigned}' => $this->buildUnsignedString(),
'{notnull}' => $this->buildNotNullString(),
'{unique}' => $this->buildUniqueString(),
'{default}' => $this->buildDefaultString(),
'{check}' => $this->buildCheckString(),
'{comment}' => $this->buildCommentString(),
'{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(),
'{append}' => $this->buildAppendString(),
];
return strtr($format, $placeholderValues);
}
Builds the default value specification for the column.
| protected buildDefaultString( ): string | ||
| return | string |
String with default value of column. |
|---|---|---|
protected function buildDefaultString()
{
$defaultValue = $this->buildDefaultValue();
if ($defaultValue === null) {
return '';
}
return ' DEFAULT ' . $defaultValue;
}
Return the default value for the column.
| protected buildDefaultValue( ): string|null | ||
| return | string|null |
String with default value of column. |
|---|---|---|
protected function buildDefaultValue()
{
if ($this->default === null) {
return $this->isNotNull === false ? 'NULL' : null;
}
switch (gettype($this->default)) {
case 'double':
// ensure type cast always has . as decimal separator in all locales
$defaultValue = StringHelper::floatToString($this->default);
break;
case 'boolean':
$defaultValue = $this->default ? 'TRUE' : 'FALSE';
break;
case 'integer':
case 'object':
$defaultValue = (string) $this->default;
break;
default:
$defaultValue = "'{$this->default}'";
}
return $defaultValue;
}
Builds the first constraint for the column. Defaults to unsupported.
| protected buildFirstString( ): string | ||
| return | string |
A string containing the FIRST constraint. |
|---|---|---|
protected function buildFirstString()
{
return '';
}
Builds the length/precision part of the column.
| protected buildLengthString( ): string |
protected function buildLengthString()
{
if ($this->length === null || $this->length === []) {
return '';
}
if (is_array($this->length)) {
$this->length = implode(',', $this->length);
}
return "({$this->length})";
}
Builds the not null constraint for the column.
| protected buildNotNullString( ): string | ||
| return | string |
Returns 'NOT NULL' if $isNotNull is true, 'NULL' if $isNotNull is false or an empty string otherwise. |
|---|---|---|
protected function buildNotNullString()
{
if ($this->isNotNull === true) {
return ' NOT NULL';
} elseif ($this->isNotNull === false) {
return ' NULL';
}
return '';
}
Builds the unique constraint for the column.
| protected buildUniqueString( ): string | ||
| return | string |
Returns string 'UNIQUE' if $isUnique is true, otherwise it returns an empty string. |
|---|---|---|
protected function buildUniqueString()
{
return $this->isUnique ? ' UNIQUE' : '';
}
Builds the unsigned string for column. Defaults to unsupported.
| protected buildUnsignedString( ): string | ||
| return | string |
A string containing UNSIGNED keyword. |
|---|---|---|
protected function buildUnsignedString()
{
return '';
}
Defined in: yii\base\BaseObject::canGetProperty()
Returns a value indicating whether a property can be read.
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
See also canSetProperty().
| public canGetProperty( string $name, boolean $checkVars = true ): boolean | ||
| $name | string |
The property name |
| $checkVars | boolean |
Whether to treat member variables as properties |
| return | boolean |
Whether the property can be read |
|---|---|---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
Returns a value indicating whether a property can be set.
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
See also canGetProperty().
| public canSetProperty( string $name, boolean $checkVars = true ): boolean | ||
| $name | string |
The property name |
| $checkVars | boolean |
Whether to treat member variables as properties |
| return | boolean |
Whether the property can be written |
|---|---|---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
Sets a CHECK constraint for the column.
| public check( string $check ): $this | ||
| $check | string |
The SQL of the |
public function check($check)
{
$this->check = $check;
return $this;
}
::class instead.
Defined in: yii\base\BaseObject::className()
Returns the fully qualified name of this class.
| public static className( ): string | ||
| return | string |
The fully qualified name of this class. |
|---|---|---|
public static function className()
{
return get_called_class();
}
Specifies the comment for column.
| public comment( string $comment ): $this | ||
| $comment | string |
The comment |
public function comment($comment)
{
$this->comment = $comment;
return $this;
}
Specify the default SQL expression for the column.
| public defaultExpression( string $default ): $this | ||
| $default | string |
The default value expression. |
public function defaultExpression($default)
{
$this->default = new Expression($default);
return $this;
}
Specify the default value for the column.
| public defaultValue( mixed $default ): $this | ||
| $default | mixed |
The default value. |
public function defaultValue($default)
{
if ($default === null) {
$this->null();
}
$this->default = $default;
return $this;
}
Adds an FIRST constraint to the column.
Note: MySQL, Oracle and Cubrid support only.
| public first( ): $this |
public function first()
{
$this->isFirst = true;
return $this;
}
| public getCategoryMap( ): array | ||
| return | array |
Mapping of abstract column types (keys) to type categories (values). |
|---|---|---|
public function getCategoryMap()
{
return static::$typeCategoryMap;
}
Returns the category of the column type.
| protected getTypeCategory( ): string | ||
| return | string |
A string containing the column type category name. |
|---|---|---|
protected function getTypeCategory()
{
return isset($this->categoryMap[$this->type]) ? $this->categoryMap[$this->type] : null;
}
Defined in: yii\base\BaseObject::hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists().
You may override this method when you implemented the php magic method __call().
| public hasMethod( string $name ): boolean | ||
| $name | string |
The method name |
| return | boolean |
Whether the method is defined |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in: yii\base\BaseObject::hasProperty()
Returns a value indicating whether a property is defined.
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
See also:
| public hasProperty( string $name, boolean $checkVars = true ): boolean | ||
| $name | string |
The property name |
| $checkVars | boolean |
Whether to treat member variables as properties |
| return | boolean |
Whether the property is defined |
|---|---|---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in: yii\base\BaseObject::init()
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
| public init( ): mixed |
public function init()
{
}
Adds a NOT NULL constraint to the column.
| public notNull( ): $this |
public function notNull()
{
$this->isNotNull = true;
return $this;
}
Adds a NULL constraint to the column.
| public null( ): $this |
public function null()
{
$this->isNotNull = false;
return $this;
}
| public setCategoryMap( array $categoryMap ): mixed | ||
| $categoryMap | array |
Mapping of abstract column types (keys) to type categories (values). |
public function setCategoryMap($categoryMap)
{
static::$typeCategoryMap = $categoryMap;
}
Adds a UNIQUE constraint to the column.
| public unique( ): $this |
public function unique()
{
$this->isUnique = true;
return $this;
}
Signup or Login in order to comment.