類別 yii\db\ColumnSchemaBuilder
ColumnSchemaBuilder 幫助使用 PHP 介面定義資料庫結構描述類型。
請參閱 yii\db\SchemaBuilderTrait 以取得更詳細的描述和使用範例。
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$after | string | 此欄位將被新增在其後的欄位。 | yii\db\ColumnSchemaBuilder |
$append | mixed | 要附加到欄位結構描述定義的 SQL 字串。 | yii\db\ColumnSchemaBuilder |
$categoryMap | array | 抽象欄位類型(鍵)到類型類別(值)的映射。 | yii\db\ColumnSchemaBuilder |
$check | string | 欄位的 `CHECK` 約束。 | yii\db\ColumnSchemaBuilder |
$comment | string | 欄位的註解值。 | yii\db\ColumnSchemaBuilder |
$db | yii\db\Connection | 目前的資料庫連線。 | yii\db\ColumnSchemaBuilder |
$default | mixed | 欄位的預設值。 | yii\db\ColumnSchemaBuilder |
$isFirst | boolean | 此欄位是否要插入到表格的開頭。 | yii\db\ColumnSchemaBuilder |
$isNotNull | boolean|null | 欄位是否可為 Null。 | yii\db\ColumnSchemaBuilder |
$isUnique | boolean | 欄位值是否應為唯一。 | yii\db\ColumnSchemaBuilder |
$isUnsigned | boolean | 欄位值是否應為無符號。 | yii\db\ColumnSchemaBuilder |
$length | integer|string|array | 欄位大小或精確度定義。 | yii\db\ColumnSchemaBuilder |
$type | string | 欄位類型定義,例如 INTEGER、VARCHAR、DATETIME 等。 | yii\db\ColumnSchemaBuilder |
$typeCategoryMap | array | 抽象欄位類型(鍵)到類型類別(值)的映射。 | yii\db\ColumnSchemaBuilder |
公共方法
受保護的方法
方法 | 描述 | 定義於 |
---|---|---|
buildAfterString() | 為資料行建立 after 約束。預設為不支援。 | yii\db\ColumnSchemaBuilder |
buildAppendString() | 建立附加到資料行定義的自訂字串。 | yii\db\ColumnSchemaBuilder |
buildCheckString() | 為資料行建立 check 約束。 | yii\db\ColumnSchemaBuilder |
buildCommentString() | 為資料行建立註解規格。 | yii\db\ColumnSchemaBuilder |
buildCompleteString() | 從輸入格式傳回完整的資料行定義。 | yii\db\ColumnSchemaBuilder |
buildDefaultString() | 為資料行建立預設值規格。 | yii\db\ColumnSchemaBuilder |
buildDefaultValue() | 傳回資料行的預設值。 | yii\db\ColumnSchemaBuilder |
buildFirstString() | 為資料行建立 first 約束。預設為不支援。 | yii\db\ColumnSchemaBuilder |
buildLengthString() | 建立資料行的長度/精確度部分。 | yii\db\ColumnSchemaBuilder |
buildNotNullString() | 為資料行建立 not null 約束。 | yii\db\ColumnSchemaBuilder |
buildUniqueString() | 為資料行建立 unique 約束。 | yii\db\ColumnSchemaBuilder |
buildUnsignedString() | 為資料行建立 unsigned 字串。預設為不支援。 | yii\db\ColumnSchemaBuilder |
getTypeCategory() | 傳回資料行類型的類別。 | yii\db\ColumnSchemaBuilder |
常數
常數 | 值 | 描述 | 定義於 |
---|---|---|---|
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 |
屬性詳細資訊
指示資料行是否可為 null。如果此值為 true
,將新增 NOT NULL
約束。如果此值為 false
,將新增 NULL
約束。
指示資料行值是否應為無號。如果此值為 true
,將新增 UNSIGNED
關鍵字。
資料行大小或精確度定義。這是資料行類型後方括號內的内容。這可以是字串、整數或陣列。如果是陣列,則陣列值將會以逗號分隔的方式聯結成字串。
抽象欄位類型(鍵)到類型類別(值)的映射。
\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,
]
方法詳細資訊
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫不明方法時 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
建立欄位結構描述產生器實例,並指定類型和值精確度。
public void __construct ( $type, $length = null, $db = null, $config = [] ) | ||
$type | string |
資料行的類型。請參閱 $type。 |
$length | integer|string|array|null |
資料行的長度或精確度。請參閱 $length。 |
$db | yii\db\Connection|null |
目前的資料庫連線。請參閱 $db。 |
$config | array |
將用於初始化物件屬性的名稱-值配對 |
public function __construct($type, $length = null, $db = null, $config = [])
{
$this->type = $type;
$this->length = $length;
$this->db = $db;
parent::__construct($config);
}
定義於: yii\base\BaseObject::__get()
傳回物件屬性的值。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,會在執行 $value = $object->property;
時隱含地呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫 |
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);
}
定義於: yii\base\BaseObject::__isset()
檢查屬性是否已設定,即已定義且不為 Null。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,會在執行 isset($object->property)
時隱含地呼叫。
請注意,如果未定義屬性,將會傳回 false。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
指示已設定 (非 null) 的具名屬性。 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定義於: yii\base\BaseObject::__set()
設定物件屬性的值。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,會在執行 $object->property = $value;
時隱含地呼叫。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀 |
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);
}
}
為欄位的結構描述建立完整字串。
public string __toString ( ) |
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);
}
定義於: yii\base\BaseObject::__unset()
將物件屬性設定為 Null。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,會在執行 unset($object->property)
時隱含地呼叫。
請注意,如果未定義屬性,此方法將不會執行任何動作。如果屬性為唯讀,則會擲回例外。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
---|
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);
}
}
為欄位新增 `AFTER` 約束。
注意:僅支援 MySQL、Oracle 和 Cubrid。
public $this after ( $after ) | ||
$after | string |
將在此資料行之後新增 $this 資料行的資料行。 |
public function after($after)
{
$this->after = $after;
return $this;
}
指定要附加到欄位定義的其他 SQL。
位置修飾符將附加在支援它們的資料庫中的資料行定義之後。
public $this append ( $sql ) | ||
$sql | string |
要附加的 SQL 字串。 |
public function append($sql)
{
$this->append = $sql;
return $this;
}
為資料行建立 after 約束。預設為不支援。
protected string buildAfterString ( ) | ||
return | string |
包含 AFTER 約束的字串。 |
---|
protected function buildAfterString()
{
return '';
}
建立附加到資料行定義的自訂字串。
protected string buildAppendString ( ) | ||
return | string |
要附加的自訂字串。 |
---|
protected function buildAppendString()
{
return $this->append !== null ? ' ' . $this->append : '';
}
為資料行建立 check 約束。
protected string buildCheckString ( ) | ||
return | string |
包含 CHECK 約束的字串。 |
---|
protected function buildCheckString()
{
return $this->check !== null ? " CHECK ({$this->check})" : '';
}
為資料行建立註解規格。
protected string buildCommentString ( ) | ||
return | string |
包含 COMMENT 關鍵字和註解本身的字串 |
---|
protected function buildCommentString()
{
return '';
}
從輸入格式傳回完整的資料行定義。
protected string buildCompleteString ( $format ) | ||
$format | string |
定義的格式。 |
return | string |
包含完整資料行定義的字串。 |
---|
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);
}
為資料行建立預設值規格。
protected string buildDefaultString ( ) | ||
return | string |
包含資料行預設值的字串。 |
---|
protected function buildDefaultString()
{
$defaultValue = $this->buildDefaultValue();
if ($defaultValue === null) {
return '';
}
return ' DEFAULT ' . $defaultValue;
}
傳回資料行的預設值。
protected string|null buildDefaultValue ( ) | ||
return | string|null |
包含資料行預設值的字串。 |
---|
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;
}
為資料行建立 first 約束。預設為不支援。
protected string buildFirstString ( ) | ||
return | string |
包含 FIRST 約束的字串。 |
---|
protected function buildFirstString()
{
return '';
}
建立資料行的長度/精確度部分。
protected string buildLengthString ( ) |
protected function buildLengthString()
{
if ($this->length === null || $this->length === []) {
return '';
}
if (is_array($this->length)) {
$this->length = implode(',', $this->length);
}
return "({$this->length})";
}
為資料行建立 not null 約束。
protected string buildNotNullString ( ) | ||
return | string |
如果 $isNotNull 為 true,則傳回 'NOT NULL';如果 $isNotNull 為 false,則傳回 'NULL';否則傳回空字串。 |
---|
protected function buildNotNullString()
{
if ($this->isNotNull === true) {
return ' NOT NULL';
} elseif ($this->isNotNull === false) {
return ' NULL';
}
return '';
}
為資料行建立 unique 約束。
protected string buildUniqueString ( ) | ||
return | string |
如果 $isUnique 為 true,則傳回字串 'UNIQUE',否則傳回空字串。 |
---|
protected function buildUniqueString()
{
return $this->isUnique ? ' UNIQUE' : '';
}
為資料行建立 unsigned 字串。預設為不支援。
protected string buildUnsignedString ( ) | ||
return | string |
包含 UNSIGNED 關鍵字的字串。 |
---|
protected function buildUnsignedString()
{
return '';
}
定義於: yii\base\BaseObject::canGetProperty()
傳回一個值,指示屬性是否可讀取。
如果屬性是可讀取的:
- 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可以讀取 |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定義於: yii\base\BaseObject::canSetProperty()
傳回一個值,指示屬性是否可設定。
如果屬性是可寫入的:
- 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可以寫入 |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
為欄位設定 `CHECK` 約束。
public $this check ( $check ) | ||
$check | string |
要加入的 CHECK 約束的 SQL。 |
public function check($check)
{
$this->check = $check;
return $this;
}
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
指定欄位的註解。
public $this comment ( $comment ) | ||
$comment | string |
註解 |
public function comment($comment)
{
$this->comment = $comment;
return $this;
}
為欄位指定預設 SQL 表達式。
public $this defaultExpression ( $default ) | ||
$default | string |
預設值表達式。 |
public function defaultExpression($default)
{
$this->default = new Expression($default);
return $this;
}
為欄位指定預設值。
public $this defaultValue ( $default ) | ||
$default | mixed |
預設值。 |
public function defaultValue($default)
{
if ($default === null) {
$this->null();
}
$this->default = $default;
return $this;
}
為資料行新增 FIRST
約束。
注意:僅支援 MySQL、Oracle 和 Cubrid。
public $this first ( ) |
public function first()
{
$this->isFirst = true;
return $this;
}
public array getCategoryMap ( ) | ||
return | array |
抽象欄位類型(鍵)到類型類別(值)的映射。 |
---|
public function getCategoryMap()
{
return static::$typeCategoryMap;
}
傳回資料行類型的類別。
protected string getTypeCategory ( ) | ||
return | string |
包含資料行類型類別名稱的字串。 |
---|
protected function getTypeCategory()
{
return isset($this->categoryMap[$this->type]) ? $this->categoryMap[$this->type] : null;
}
定義於: yii\base\BaseObject::hasMethod()
傳回一個值,指示是否已定義方法。
預設實作是對 php 函數 method_exists() 的呼叫。當您實作 php magic method __call() 時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | string |
方法名稱 |
return | boolean |
方法是否已定義 |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定義於: yii\base\BaseObject::hasProperty()
傳回一個值,指示是否已定義屬性。
如果屬性已定義:
- 類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
public void init ( ) |
public function init()
{
}
為資料行新增 NOT NULL
約束。
public $this notNull ( ) |
public function notNull()
{
$this->isNotNull = true;
return $this;
}
為資料行新增 NULL
約束。
public $this null ( ) |
public function null()
{
$this->isNotNull = false;
return $this;
}
public void setCategoryMap ( $categoryMap ) | ||
$categoryMap | array |
抽象欄位類型(鍵)到類型類別(值)的映射。 |
public function setCategoryMap($categoryMap)
{
static::$typeCategoryMap = $categoryMap;
}
為資料行新增 UNIQUE
約束。
public $this unique ( ) |
public function unique()
{
$this->isUnique = true;
return $this;
}
註冊 或 登入 以進行評論。