1 關注者

類別 yii\db\Migration

繼承yii\db\Migration » yii\base\Component » yii\base\BaseObject
實作yii\base\Configurable, yii\db\MigrationInterface
使用 Traitsyii\db\SchemaBuilderTrait
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/db/Migration.php

Migration 是表示資料庫遷移的基底類別。

Migration 設計用於與 “yii migrate” 命令一起使用。

Migration 的每個子類別都代表一個個別的資料庫遷移,並以子類別名稱識別。

在每個遷移中,應覆寫 up() 方法以包含「升級」資料庫的邏輯;而 down() 方法則包含「降級」邏輯。

如果資料庫支援交易,您也可以覆寫 safeUp()safeDown(),以便在升級或降級期間發生任何錯誤時,可以完全還原整個遷移。

請注意,某些 DBMS 中的某些資料庫查詢無法放入交易中。如需範例,請參閱 隱含提交。如果是這種情況,您仍然應該實作 up()down(),而不是 safeUp()safeDown()

Migration 提供了一組方便的方法來操作資料庫資料和綱要。例如,insert() 方法可用於輕鬆地將資料列插入資料庫表格中;createTable() 方法可用於建立資料庫表格。與 yii\db\Command 中的相同方法相比,這些方法將顯示額外資訊,顯示方法參數和執行時間,這在應用遷移時可能很有用。

有關 Migration 的更多詳細資訊和使用資訊,請參閱關於遷移的指南文章

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$behaviors yii\base\Behavior[] 附加到此組件的行為列表。 yii\base\Component
$compact boolean 指示是否應壓縮控制台輸出。 yii\db\Migration
$db yii\db\Connection|array|string 此遷移應使用的 DB 連線物件或 DB 連線的應用程式組件 ID。 yii\db\Migration
$maxSqlOutputLength integer 輸出的 SQL 的最大字元數。 yii\db\Migration

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫指定的非類別方法。 yii\base\Component
__clone() 在透過複製現有物件建立物件後,會呼叫此方法。 yii\base\Component
__construct() 建構子。 yii\base\BaseObject
__get() 傳回組件屬性的值。 yii\base\Component
__isset() 檢查是否已設定屬性,即已定義且非 null。 yii\base\Component
__set() 設定組件屬性的值。 yii\base\Component
__unset() 將組件屬性設定為 null。 yii\base\Component
addColumn() 建立並執行 SQL 陳述式以新增 DB 資料行。 yii\db\Migration
addCommentOnColumn() 建立並執行 SQL 陳述式以新增資料行註解。 yii\db\Migration
addCommentOnTable() 建立 SQL 陳述式以新增表格註解。 yii\db\Migration
addForeignKey() 建立 SQL 陳述式以將外鍵約束新增至現有表格。 yii\db\Migration
addPrimaryKey() 建立並執行 SQL 陳述式以建立主鍵。 yii\db\Migration
alterColumn() 建立並執行 SQL 陳述式以變更資料行的定義。 yii\db\Migration
attachBehavior() 將行為附加到此組件。 yii\base\Component
attachBehaviors() 將行為列表附加到組件。 yii\base\Component
batchInsert() 建立並執行批次 INSERT SQL 陳述式。 yii\db\Migration
behaviors() 傳回此組件應表現為的行為列表。 yii\base\Component
bigInteger() 建立 bigint 資料行。 yii\db\SchemaBuilderTrait
bigPrimaryKey() 建立 big 主鍵資料行。 yii\db\SchemaBuilderTrait
binary() 建立 binary 資料行。 yii\db\SchemaBuilderTrait
boolean() 建立 boolean 資料行。 yii\db\SchemaBuilderTrait
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\Component
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\Component
char() 建立 char 欄位。 yii\db\SchemaBuilderTrait
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
createIndex() 建立並執行用於建立新索引的 SQL 陳述式。 yii\db\Migration
createTable() 建立並執行用於建立新資料庫表格的 SQL 陳述式。 yii\db\Migration
date() 建立日期欄位。 yii\db\SchemaBuilderTrait
dateTime() 建立日期時間欄位。 yii\db\SchemaBuilderTrait
decimal() 建立 decimal 欄位。 yii\db\SchemaBuilderTrait
delete() 建立並執行 DELETE SQL 陳述式。 yii\db\Migration
detachBehavior() 從元件卸離行為。 yii\base\Component
detachBehaviors() 從元件卸離所有行為。 yii\base\Component
double() 建立 double 欄位。 yii\db\SchemaBuilderTrait
down() 此方法包含移除此遷移時要執行的邏輯。 yii\db\Migration
dropColumn() 建立並執行用於刪除資料庫欄位的 SQL 陳述式。 yii\db\Migration
dropCommentFromColumn() 建立並執行用於從欄位刪除註解的 SQL 陳述式。 yii\db\Migration
dropCommentFromTable() 建立用於從表格刪除註解的 SQL 陳述式。 yii\db\Migration
dropForeignKey() 建立用於刪除外鍵約束的 SQL 陳述式。 yii\db\Migration
dropIndex() 建立並執行用於刪除索引的 SQL 陳述式。 yii\db\Migration
dropPrimaryKey() 建立並執行用於刪除主鍵的 SQL 陳述式。 yii\db\Migration
dropTable() 建立並執行用於刪除資料庫表格的 SQL 陳述式。 yii\db\Migration
ensureBehaviors() 確保在 behaviors() 中宣告的行為已附加到此元件。 yii\base\Component
execute() 執行 SQL 陳述式。 yii\db\Migration
float() 建立 float 欄位。 yii\db\SchemaBuilderTrait
getBehavior() 傳回具名行為物件。 yii\base\Component
getBehaviors() 傳回附加到此元件的所有行為。 yii\base\Component
hasEventHandlers() 傳回一個值,指示是否有任何處理常式附加到具名事件。 yii\base\Component
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\Component
hasProperty() 傳回一個值,指示是否為此元件定義了屬性。 yii\base\Component
init() 初始化遷移。 yii\db\Migration
insert() 建立並執行 INSERT SQL 陳述式。 yii\db\Migration
integer() 建立 integer 欄位。 yii\db\SchemaBuilderTrait
json() 建立 JSON 欄位。 yii\db\SchemaBuilderTrait
money() 建立 money 欄位。 yii\db\SchemaBuilderTrait
off() 從此元件卸離現有的事件處理常式。 yii\base\Component
on() 將事件處理常式附加到事件。 yii\base\Component
primaryKey() 建立主鍵欄位。 yii\db\SchemaBuilderTrait
renameColumn() 建立並執行用於重新命名欄位的 SQL 陳述式。 yii\db\Migration
renameTable() 建立並執行用於重新命名資料庫表格的 SQL 陳述式。 yii\db\Migration
safeDown() 此方法包含移除此遷移時要執行的邏輯。 yii\db\Migration
safeUp() 此方法包含套用此遷移時要執行的邏輯。 yii\db\Migration
smallInteger() 建立 smallint 欄位。 yii\db\SchemaBuilderTrait
string() 建立字串欄位。 yii\db\SchemaBuilderTrait
text() 建立 text 欄位。 yii\db\SchemaBuilderTrait
time() 建立時間欄位。 yii\db\SchemaBuilderTrait
timestamp() 建立時間戳記欄位。 yii\db\SchemaBuilderTrait
tinyInteger() 建立 tinyint 欄位。如果 DBMS 不支援 tinyint,將會使用 smallint。 yii\db\SchemaBuilderTrait
trigger() 觸發事件。 yii\base\Component
truncateTable() 建立並執行用於截斷資料庫表格的 SQL 陳述式。 yii\db\Migration
up() 此方法包含套用此遷移時要執行的邏輯。 yii\db\Migration
update() 建立並執行 UPDATE SQL 陳述式。 yii\db\Migration
upsert() 建立並執行命令,以在資料庫表格中插入列(如果它們尚不存在,符合唯一約束),或者在它們存在時更新它們。 yii\db\Migration

受保護的方法

隱藏繼承的方法

方法 描述 定義於
beginCommand() 準備要執行的命令,並輸出到主控台。 yii\db\Migration
endCommand() 在命令執行後完成,並將經過的時間輸出到主控台。 yii\db\Migration
getDb() yii\db\Migration

屬性詳情

隱藏繼承的屬性

$compact 公開屬性 (自版本 2.0.13 起可用)

指示是否應壓縮主控台輸出。如果設定為 true,則遷移中執行的個別命令將不會輸出到主控台。預設值為 false,換句話說,預設情況下輸出是完全詳細的。

public boolean $compact false
$db 公開屬性

DB 連線物件或此遷移應使用的 DB 連線的應用程式元件 ID。從版本 2.0.2 開始,這也可以是用於建立物件的組態陣列。

請注意,當 Migration 物件由 migrate 命令建立時,此屬性將被命令覆寫。如果您不想使用命令提供的 DB 連線,您可以覆寫 init() 方法,如下所示

public function init()
{
    $this->db = 'db2';
    parent::init();
}
$maxSqlOutputLength 公開屬性 (自版本 2.0.13 起可用)

輸出的 SQL 的最大字元數。適用於減少長語句並使主控台輸出更精簡。

方法詳情

隱藏繼承的方法

__call() 公開方法

定義於: yii\base\Component::__call()

呼叫指定的非類別方法。

此方法將檢查是否有任何附加的行為具有具名方法,並在可用時執行它。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當呼叫不明方法時,將會隱式呼叫它。

public mixed __call ( $name, $params )
$name 字串

方法名稱

$params 陣列

方法參數

傳回 混合

方法傳回值

拋出 yii\base\UnknownMethodException

當呼叫不明方法時

                public function __call($name, $params)
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $object) {
        if ($object->hasMethod($name)) {
            return call_user_func_array([$object, $name], $params);
        }
    }
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__clone() 公開方法

定義於: yii\base\Component::__clone()

在透過複製現有物件建立物件後,會呼叫此方法。

它會移除所有行為,因為它們附加到舊物件。

public void __clone ( )

                public function __clone()
{
    $this->_events = [];
    $this->_eventWildcards = [];
    $this->_behaviors = null;
}

            
__construct() 公開方法

定義於: yii\base\BaseObject::__construct()

建構子。

預設實作執行兩件事

  • 使用給定的組態 $config 初始化物件。
  • 呼叫 init()

如果此方法在子類別中被覆寫,建議

  • 建構子的最後一個參數是組態陣列,例如此處的 $config
  • 在建構子結尾呼叫父層實作。
public void __construct ( $config = [] )
$config 陣列

將用於初始化物件屬性的名稱-值對

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() 公開方法

定義於: yii\base\Component::__get()

傳回組件屬性的值。

此方法將依下列順序檢查並採取相應的動作

  • getter 定義的屬性:傳回 getter 結果
  • 行為的屬性:傳回行為屬性值

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $component->property; 時,將會隱式呼叫它。

另請參閱 __set()

public mixed __get ( $name )
$name 字串

屬性名稱

傳回 混合

屬性值或行為屬性的值

拋出 yii\base\UnknownPropertyException

如果未定義屬性

拋出 yii\base\InvalidCallException

如果屬性為唯寫。

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        // read property, e.g. getName()
        return $this->$getter();
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name;
        }
    }
    if (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);
}

            
__isset() 公開方法

定義於: yii\base\Component::__isset()

檢查是否已設定屬性,即已定義且非 null。

此方法將依下列順序檢查並採取相應的動作

  • setter 定義的屬性:傳回屬性是否已設定
  • 行為的屬性:傳回屬性是否已設定
  • 對於不存在的屬性,傳回 false

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,將會隱式呼叫它。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name 字串

屬性名稱或事件名稱

傳回 boolean

具名屬性是否已設定

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name !== null;
        }
    }
    return false;
}

            
__set() 公開方法

定義於: yii\base\Component::__set()

設定組件屬性的值。

此方法將依下列順序檢查並採取相應的動作

  • setter 定義的屬性:設定屬性值
  • 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
  • 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
  • 行為的屬性:設定行為屬性值

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $component->property = $value; 時,將會隱式呼叫它。

另請參閱 __get()

public void __set ( $name, $value )
$name 字串

屬性名稱或事件名稱

$value 混合

屬性值

拋出 yii\base\UnknownPropertyException

如果未定義屬性

拋出 yii\base\InvalidCallException

如果屬性為唯讀。

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // set property
        $this->$setter($value);
        return;
    } elseif (strncmp($name, 'on ', 3) === 0) {
        // on event: attach event handler
        $this->on(trim(substr($name, 3)), $value);
        return;
    } elseif (strncmp($name, 'as ', 3) === 0) {
        // as behavior: attach behavior
        $name = trim(substr($name, 3));
        $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = $value;
            return;
        }
    }
    if (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}

            
__unset() 公開方法

定義於: yii\base\Component::__unset()

將組件屬性設定為 null。

此方法將依下列順序檢查並採取相應的動作

  • setter 定義的屬性:將屬性值設定為 null
  • 行為的屬性:將屬性值設定為 null

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 unset($component->property) 時,將會隱式呼叫它。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name 字串

屬性名稱

拋出 yii\base\InvalidCallException

如果屬性為唯讀。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = null;
            return;
        }
    }
    throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}

            
addColumn() 公開方法

建立並執行 SQL 陳述式以新增 DB 資料行。

public void addColumn ( $table, $column, $type )
$table 字串

新欄位將新增至的表格。表格名稱將由方法正確地引用。

$column 字串

新欄位的名稱。名稱將由方法正確地引用。

$type 字串

欄位類型。yii\db\QueryBuilder::getColumnType() 方法將被調用,以將抽象欄位類型(如果有的話)轉換為實體類型。任何未被識別為抽象類型的內容都將保留在產生的 SQL 中。例如,'string' 將轉換為 'varchar(255)',而 'string not null' 將變為 'varchar(255) not null'。

                public function addColumn($table, $column, $type)
{
    $time = $this->beginCommand("add column $column $type to table $table");
    $this->db->createCommand()->addColumn($table, $column, $type)->execute();
    if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
        $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
    }
    $this->endCommand($time);
}

            
addCommentOnColumn() 公開方法 (自版本 2.0.8 起可用)

建立並執行 SQL 陳述式以新增資料行註解。

public void addCommentOnColumn ( $table, $column, $comment )
$table 字串

要註解其欄位的表格。表格名稱將由方法正確地引用。

$column 字串

要註解的欄位名稱。欄位名稱將由方法正確地引用。

$comment 字串

要新增的註解文字。註解將由方法正確地引用。

                public function addCommentOnColumn($table, $column, $comment)
{
    $time = $this->beginCommand("add comment on column $column");
    $this->db->createCommand()->addCommentOnColumn($table, $column, $comment)->execute();
    $this->endCommand($time);
}

            
addCommentOnTable() 公開方法 (自版本 2.0.8 起可用)

建立 SQL 陳述式以新增表格註解。

public void addCommentOnTable ( $table, $comment )
$table 字串

要註解的表格。表格名稱將由方法正確地引用。

$comment 字串

要新增的註解文字。註解將由方法正確地引用。

                public function addCommentOnTable($table, $comment)
{
    $time = $this->beginCommand("add comment on table $table");
    $this->db->createCommand()->addCommentOnTable($table, $comment)->execute();
    $this->endCommand($time);
}

            
addForeignKey() 公開方法

建立 SQL 陳述式以將外鍵約束新增至現有表格。

此方法將正確地引用表格和欄位名稱。

public void addForeignKey ( $name, $table, $columns, $refTable, $refColumns, $delete null, $update null )
$name 字串

外鍵約束的名稱。

$table 字串

外鍵約束將新增至的表格。

$columns string|array

約束將新增至的欄位名稱。如果有多個欄位,請用逗號分隔或使用陣列。

$refTable 字串

外鍵參考的表格。

$refColumns string|array

外鍵參考的欄位名稱。如果有多個欄位,請用逗號分隔或使用陣列。

$delete string|null

ON DELETE 選項。大多數 DBMS 支援這些選項:RESTRICT、CASCADE、NO ACTION、SET DEFAULT、SET NULL

$update string|null

ON UPDATE 選項。大多數 DBMS 支援這些選項:RESTRICT、CASCADE、NO ACTION、SET DEFAULT、SET NULL

                public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
    $time = $this->beginCommand("add foreign key $name: $table (" . implode(',', (array) $columns) . ") references $refTable (" . implode(',', (array) $refColumns) . ')');
    $this->db->createCommand()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update)->execute();
    $this->endCommand($time);
}

            
addPrimaryKey() 公開方法

建立並執行 SQL 陳述式以建立主鍵。

此方法將正確地引用表格和欄位名稱。

public void addPrimaryKey ( $name, $table, $columns )
$name 字串

主鍵約束的名稱。

$table 字串

主鍵約束將新增至的表格。

$columns string|array

主鍵將由其組成的逗號分隔字串或欄位陣列。

                public function addPrimaryKey($name, $table, $columns)
{
    $time = $this->beginCommand("add primary key $name on $table (" . (is_array($columns) ? implode(',', $columns) : $columns) . ')');
    $this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
    $this->endCommand($time);
}

            
alterColumn() 公開方法

建立並執行 SQL 陳述式以變更資料行的定義。

public void alterColumn ( $table, $column, $type )
$table 字串

要變更其欄位的表格。表格名稱將由方法正確地引用。

$column 字串

要變更的欄位名稱。名稱將由方法正確地引用。

$type 字串

新的欄位類型。yii\db\QueryBuilder::getColumnType() 方法將被調用,以將抽象欄位類型(如果有的話)轉換為實體類型。任何未被識別為抽象類型的內容都將保留在產生的 SQL 中。例如,'string' 將轉換為 'varchar(255)',而 'string not null' 將變為 'varchar(255) not null'。

                public function alterColumn($table, $column, $type)
{
    $time = $this->beginCommand("alter column $column in table $table to $type");
    $this->db->createCommand()->alterColumn($table, $column, $type)->execute();
    if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
        $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
    }
    $this->endCommand($time);
}

            
attachBehavior() 公開方法

定義於: yii\base\Component::attachBehavior()

將行為附加到此組件。

此方法將根據給定的組態建立行為物件。之後,將透過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此元件。

另請參閱 detachBehavior()

public yii\base\Behavior attachBehavior ( $name, $behavior )
$name 字串

行為的名稱。

$behavior string|array|yii\base\Behavior

行為組態。這可以是下列其中之一

傳回 yii\base\Behavior

行為物件

                public function attachBehavior($name, $behavior)
{
    $this->ensureBehaviors();
    return $this->attachBehaviorInternal($name, $behavior);
}

            
attachBehaviors() 公開方法

定義於: yii\base\Component::attachBehaviors()

將行為列表附加到組件。

每個行為都依其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或用於建立行為的組態陣列。

另請參閱 attachBehavior()

public void attachBehaviors ( $behaviors )
$behaviors 陣列

要附加到元件的行為清單

                public function attachBehaviors($behaviors)
{
    $this->ensureBehaviors();
    foreach ($behaviors as $name => $behavior) {
        $this->attachBehaviorInternal($name, $behavior);
    }
}

            
batchInsert() 公開方法

建立並執行批次 INSERT SQL 陳述式。

此方法將正確地逸出欄位名稱,並繫結要插入的值。

public void batchInsert ( $table, $columns, $rows )
$table 字串

新列將插入的表格。

$columns 陣列

欄位名稱。

$rows 陣列

要批次插入表格的列

                public function batchInsert($table, $columns, $rows)
{
    $time = $this->beginCommand("insert into $table");
    $this->db->createCommand()->batchInsert($table, $columns, $rows)->execute();
    $this->endCommand($time);
}

            
beginCommand() 受保護的方法 (自版本 2.0.13 起可用)

準備要執行的命令,並輸出到主控台。

protected float beginCommand ( $description )
$description 字串

命令的描述,將輸出到主控台。

傳回 float

命令執行前的時間,用於計算經過的時間。

                protected function beginCommand($description)
{
    if (!$this->compact) {
        echo "    > $description ...";
    }
    return microtime(true);
}

            
behaviors() 公開方法

定義於: yii\base\Component::behaviors()

傳回此組件應表現為的行為列表。

子類別可以覆寫此方法,以指定它們想要表現的行為。

此方法的傳回值應該是由行為名稱索引的行為物件或組態陣列。行為組態可以是指定行為類別的字串,或具有下列結構的陣列

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

請注意,行為類別必須從 yii\base\Behavior 擴展。行為可以使用名稱或匿名方式附加。當名稱用作陣列鍵時,使用此名稱,稍後可以使用 getBehavior() 檢索行為,或使用 detachBehavior() 卸離行為。匿名行為無法檢索或卸離。

在此方法中宣告的行為將自動(按需)附加到元件。

public array behaviors ( )
傳回 陣列

行為組態。

                public function behaviors()
{
    return [];
}

            
bigInteger() 公開方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::bigInteger()

建立 bigint 資料行。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
bigPrimaryKey() 公開方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::bigPrimaryKey()

建立 big 主鍵資料行。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
binary() 公開方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::binary()

建立 binary 資料行。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
boolean() 公開方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::boolean()

建立 boolean 資料行。

public yii\db\ColumnSchemaBuilder boolean ( )
傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
canGetProperty() public 方法

定義於: yii\base\Component::canGetProperty()

傳回一個值,指示是否可以讀取屬性。

若符合以下條件,屬性即可讀取:

  • 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有給定名稱的可讀屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此組件的屬性

傳回 boolean

屬性是否可以被讀取

                public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canGetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
canSetProperty() public 方法

定義於: yii\base\Component::canSetProperty()

傳回一個值,指示是否可以設定屬性。

若符合以下條件,屬性即可寫入:

  • 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有給定名稱的可寫屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此組件的屬性

傳回 boolean

屬性是否可以被寫入

                public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canSetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
char() public 方法 (自版本 2.0.8 起可用)

定義於: yii\db\SchemaBuilderTrait::char()

建立 char 欄位。

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

欄位大小定義,即最大字串長度。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
className() public 靜態方法
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
傳回 字串

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
createIndex() public 方法

建立並執行用於建立新索引的 SQL 陳述式。

public void createIndex ( $name, $table, $columns, $unique false )
$name 字串

索引的名稱。名稱將由方法正確地加上引號。

$table 字串

將在其上建立新索引的資料表。資料表名稱將由方法正確地加上引號。

$columns string|array

應包含在索引中的欄位。如果有多個欄位,請用逗號分隔或使用陣列。每個欄位名稱將由方法正確地加上引號。對於包含左括號 "(" 的欄位名稱,將跳過引號。

$unique boolean

是否在新建立的索引上新增 UNIQUE 限制。

                public function createIndex($name, $table, $columns, $unique = false)
{
    $time = $this->beginCommand('create' . ($unique ? ' unique' : '') . " index $name on $table (" . implode(',', (array) $columns) . ')');
    $this->db->createCommand()->createIndex($name, $table, $columns, $unique)->execute();
    $this->endCommand($time);
}

            
createTable() public 方法

建立並執行用於建立新資料庫表格的 SQL 陳述式。

新資料表中的欄位應指定為名稱-定義配對(例如 'name' => 'string'),其中 name 代表欄位名稱,該名稱將由方法正確地加上引號,而 definition 代表欄位類型,該類型必須包含抽象 DB 類型。

將調用 yii\db\QueryBuilder::getColumnType() 方法,將任何抽象類型轉換為物理類型。

如果僅使用定義指定欄位(例如 'PRIMARY KEY (name, type)'),它將直接放入產生的 SQL 中。

使用範例: `php class m200000_000000_create_table_fruits extends \yii\db\Migration {

public function safeUp()
{
     $this->createTable('{{%fruits}}', [
         // ...
         'column_name double precision null default null',

public void createTable ( $table, $columns, $options null )
$table 字串

要建立的資料表名稱。名稱將由方法正確地加上引號。

$columns 陣列

新資料表中的欄位(name => definition)。

$options string|null

將附加到產生的 SQL 的其他 SQL 片段。

                public function createTable($table, $columns, $options = null)
{
    $time = $this->beginCommand("create table $table");
    $this->db->createCommand()->createTable($table, $columns, $options)->execute();
    foreach ($columns as $column => $type) {
        if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
            $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
        }
    }
    $this->endCommand($time);
}

            
date() public 方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::date()

建立日期欄位。

public yii\db\ColumnSchemaBuilder date ( )
傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
dateTime() public 方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::dateTime()

建立日期時間欄位。

public yii\db\ColumnSchemaBuilder dateTime ( $precision null )
$precision integer|null

欄位值精確度。傳遞給欄位類型的第一個參數,例如 DATETIME(precision)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
decimal() public 方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::decimal()

建立 decimal 欄位。

public yii\db\ColumnSchemaBuilder decimal ( $precision null, $scale null )
$precision integer|null

欄位值精確度,通常是總位數。傳遞給欄位類型的第一個參數,例如 DECIMAL(precision, scale)。如果 DBMS 不支援此參數,則會忽略此參數。

$scale integer|null

欄位值小數位數,通常是小數點後的位數。傳遞給欄位類型的第二個參數,例如 DECIMAL(precision, scale)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

                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);
}

            
delete() public 方法

建立並執行 DELETE SQL 陳述式。

public void delete ( $table, $condition '', $params = [] )
$table 字串

將從中刪除資料的資料表。

$condition array|string

將放入 WHERE 部分的條件。請參閱 yii\db\Query::where(),了解如何指定條件。

$params 陣列

要綁定到查詢的參數。

                public function delete($table, $condition = '', $params = [])
{
    $time = $this->beginCommand("delete from $table");
    $this->db->createCommand()->delete($table, $condition, $params)->execute();
    $this->endCommand($time);
}

            
detachBehavior() public 方法

定義於: yii\base\Component::detachBehavior()

從元件卸離行為。

將調用行為的 yii\base\Behavior::detach() 方法。

public yii\base\Behavior|null detachBehavior ( $name )
$name 字串

行為的名稱。

傳回 yii\base\Behavior|null

已分離的行為。如果行為不存在,則為 Null。

                public function detachBehavior($name)
{
    $this->ensureBehaviors();
    if (isset($this->_behaviors[$name])) {
        $behavior = $this->_behaviors[$name];
        unset($this->_behaviors[$name]);
        $behavior->detach();
        return $behavior;
    }
    return null;
}

            
detachBehaviors() public 方法

定義於: yii\base\Component::detachBehaviors()

從元件卸離所有行為。

public void detachBehaviors ( )

                public function detachBehaviors()
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $name => $behavior) {
        $this->detachBehavior($name);
    }
}

            
double() public 方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::double()

建立 double 欄位。

public yii\db\ColumnSchemaBuilder double ( $precision null )
$precision integer|null

欄位值精確度。傳遞給欄位類型的第一個參數,例如 DOUBLE(precision)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
down() public 方法

此方法包含移除此遷移時要執行的邏輯。

預設實作會拋出例外,指出無法移除遷移。如果可以移除對應的遷移,子類別可以覆寫此方法。

public false|void|mixed down ( )
傳回 false|void|mixed

傳回 false 值以指示遷移失敗且不應繼續進行。所有其他傳回值表示遷移成功。

                public function down()
{
    $transaction = $this->db->beginTransaction();
    try {
        if ($this->safeDown() === false) {
            $transaction->rollBack();
            return false;
        }
        $transaction->commit();
    } catch (\Exception $e) {
        $this->printException($e);
        $transaction->rollBack();
        return false;
    } catch (\Throwable $e) {
        $this->printException($e);
        $transaction->rollBack();
        return false;
    }
    return null;
}

            
dropColumn() public 方法

建立並執行用於刪除資料庫欄位的 SQL 陳述式。

public void dropColumn ( $table, $column )
$table 字串

要刪除欄位的資料表。名稱將由方法正確地加上引號。

$column 字串

要刪除的欄位名稱。名稱將由方法正確地加上引號。

                public function dropColumn($table, $column)
{
    $time = $this->beginCommand("drop column $column from table $table");
    $this->db->createCommand()->dropColumn($table, $column)->execute();
    $this->endCommand($time);
}

            
dropCommentFromColumn() public 方法 (自版本 2.0.8 起可用)

建立並執行用於從欄位刪除註解的 SQL 陳述式。

public void dropCommentFromColumn ( $table, $column )
$table 字串

要註解其欄位的表格。表格名稱將由方法正確地引用。

$column 字串

要註解的欄位名稱。欄位名稱將由方法正確地引用。

                public function dropCommentFromColumn($table, $column)
{
    $time = $this->beginCommand("drop comment from column $column");
    $this->db->createCommand()->dropCommentFromColumn($table, $column)->execute();
    $this->endCommand($time);
}

            
dropCommentFromTable() public 方法 (自版本 2.0.8 起可用)

建立用於從表格刪除註解的 SQL 陳述式。

public void dropCommentFromTable ( $table )
$table 字串

要註解其欄位的表格。表格名稱將由方法正確地引用。

                public function dropCommentFromTable($table)
{
    $time = $this->beginCommand("drop comment from table $table");
    $this->db->createCommand()->dropCommentFromTable($table)->execute();
    $this->endCommand($time);
}

            
dropForeignKey() public 方法

建立用於刪除外鍵約束的 SQL 陳述式。

public void dropForeignKey ( $name, $table )
$name 字串

要刪除的外鍵約束的名稱。名稱將由方法正確地加上引號。

$table 字串

要刪除外鍵的資料表。名稱將由方法正確地加上引號。

                public function dropForeignKey($name, $table)
{
    $time = $this->beginCommand("drop foreign key $name from table $table");
    $this->db->createCommand()->dropForeignKey($name, $table)->execute();
    $this->endCommand($time);
}

            
dropIndex() public 方法

建立並執行用於刪除索引的 SQL 陳述式。

public void dropIndex ( $name, $table )
$name 字串

要刪除的索引名稱。名稱將由方法正確地加上引號。

$table 字串

要刪除索引的資料表。名稱將由方法正確地加上引號。

                public function dropIndex($name, $table)
{
    $time = $this->beginCommand("drop index $name on $table");
    $this->db->createCommand()->dropIndex($name, $table)->execute();
    $this->endCommand($time);
}

            
dropPrimaryKey() public 方法

建立並執行用於刪除主鍵的 SQL 陳述式。

public void dropPrimaryKey ( $name, $table )
$name 字串

要移除的主鍵約束的名稱。

$table 字串

將從中移除主鍵約束的資料表。

                public function dropPrimaryKey($name, $table)
{
    $time = $this->beginCommand("drop primary key $name");
    $this->db->createCommand()->dropPrimaryKey($name, $table)->execute();
    $this->endCommand($time);
}

            
dropTable() public 方法

建立並執行用於刪除資料庫表格的 SQL 陳述式。

public void dropTable ( $table )
$table 字串

要刪除的資料表。名稱將由方法正確地加上引號。

                public function dropTable($table)
{
    $time = $this->beginCommand("drop table $table");
    $this->db->createCommand()->dropTable($table)->execute();
    $this->endCommand($time);
}

            
endCommand() protected 方法 (自版本 2.0.13 起可用)

在命令執行後完成,並將經過的時間輸出到主控台。

protected void endCommand ( $time )
$time float

命令執行前的時間。

                protected function endCommand($time)
{
    if (!$this->compact) {
        echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    }
}

            
ensureBehaviors() public 方法

定義於: yii\base\Component::ensureBehaviors()

確保在 behaviors() 中宣告的行為已附加到此元件。

public void ensureBehaviors ( )

                public function ensureBehaviors()
{
    if ($this->_behaviors === null) {
        $this->_behaviors = [];
        foreach ($this->behaviors() as $name => $behavior) {
            $this->attachBehaviorInternal($name, $behavior);
        }
    }
}

            
execute() public 方法

執行 SQL 陳述式。

此方法使用 $db 執行指定的 SQL 陳述式。

public void execute ( $sql, $params = [] )
$sql 字串

要執行的 SQL 陳述式

$params 陣列

SQL 執行的輸入參數(name => value)。 有關更多詳細資訊,請參閱 yii\db\Command::execute()

                public function execute($sql, $params = [])
{
    $sqlOutput = $sql;
    if ($this->maxSqlOutputLength !== null) {
        $sqlOutput = StringHelper::truncate($sql, $this->maxSqlOutputLength, '[... hidden]');
    }
    $time = $this->beginCommand("execute SQL: $sqlOutput");
    $this->db->createCommand($sql)->bindValues($params)->execute();
    $this->endCommand($time);
}

            
float() public 方法 (自版本 2.0.6 起可用)

定義於: yii\db\SchemaBuilderTrait::float()

建立 float 欄位。

public yii\db\ColumnSchemaBuilder float ( $precision null )
$precision integer|null

欄位值精確度。傳遞給欄位類型的第一個參數,例如 FLOAT(precision)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
getBehavior() public 方法

定義於: yii\base\Component::getBehavior()

傳回具名行為物件。

public yii\base\Behavior|null getBehavior ( $name )
$name 字串

行為名稱

傳回 yii\base\Behavior|null

行為物件,如果行為不存在,則為 null

                public function getBehavior($name)
{
    $this->ensureBehaviors();
    return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}

            
getBehaviors() public 方法

定義於: yii\base\Component::getBehaviors()

傳回附加到此元件的所有行為。

public yii\base\Behavior[] getBehaviors ( )
傳回 yii\base\Behavior[]

附加到此組件的行為列表

                public function getBehaviors()
{
    $this->ensureBehaviors();
    return $this->_behaviors;
}

            
getDb() protected method (available since version 2.0.6)

protected void getDb ( )

                protected function getDb()
{
    return $this->db;
}

            
hasEventHandlers() public method

Defined in: yii\base\Component::hasEventHandlers()

傳回一個值,指示是否有任何處理常式附加到具名事件。

public boolean hasEventHandlers ( $name )
$name 字串

事件名稱

傳回 boolean

是否有任何處理器附加到此事件。

                public function hasEventHandlers($name)
{
    $this->ensureBehaviors();
    if (!empty($this->_events[$name])) {
        return true;
    }
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
            return true;
        }
    }
    return Event::hasHandlers($this, $name);
}

            
hasMethod() public method

Defined in: yii\base\Component::hasMethod()

傳回一個值,指示是否已定義方法。

如果滿足以下條件,則定義了方法:

  • 類別具有指定名稱的方法
  • 附加的行為具有給定名稱的方法(當 $checkBehaviors 為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors true )
$name 字串

屬性名稱

$checkBehaviors boolean

是否將行為的方法視為此元件的方法

傳回 boolean

方法是否已定義

                public function hasMethod($name, $checkBehaviors = true)
{
    if (method_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->hasMethod($name)) {
                return true;
            }
        }
    }
    return false;
}

            
hasProperty() public method

Defined in: yii\base\Component::hasProperty()

傳回一個值,指示是否為此元件定義了屬性。

如果滿足以下條件,則定義了屬性:

  • 類別具有與指定名稱關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有帶有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有給定名稱的屬性(當 $checkBehaviors 為 true 時)。

參見

public boolean hasProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此組件的屬性

傳回 boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
    return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}

            
init() public method

初始化遷移。

如果 $dbnull,此方法會將其設定為 'db' 應用程式元件。

public void init ( )

                public function init()
{
    parent::init();
    $this->db = Instance::ensure($this->db, Connection::className());
    $this->db->getSchema()->refresh();
    $this->db->enableSlaves = false;
}

            
insert() public method

建立並執行 INSERT SQL 陳述式。

此方法將正確地逸出欄位名稱,並繫結要插入的值。

public void insert ( $table, $columns )
$table 字串

新列將插入的表格。

$columns 陣列

要插入表格的欄位資料 (名稱 => 值)。

                public function insert($table, $columns)
{
    $time = $this->beginCommand("insert into $table");
    $this->db->createCommand()->insert($table, $columns)->execute();
    $this->endCommand($time);
}

            
integer() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::integer()

建立 integer 欄位。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
json() public method (available since version 2.0.14)

Defined in: yii\db\SchemaBuilderTrait::json()

建立 JSON 欄位。

public yii\db\ColumnSchemaBuilder json ( )
傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

拋出 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)

Defined in: yii\db\SchemaBuilderTrait::money()

建立 money 欄位。

public yii\db\ColumnSchemaBuilder money ( $precision null, $scale null )
$precision integer|null

欄位值精確度,通常是總位數。傳遞給欄位類型的第一個參數,例如 DECIMAL(precision, scale)。如果 DBMS 不支援此參數,則會忽略此參數。

$scale integer|null

欄位值小數位數,通常是小數點後的位數。傳遞給欄位類型的第二個參數,例如 DECIMAL(precision, scale)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

                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);
}

            
off() public method

Defined in: yii\base\Component::off()

從此元件卸離現有的事件處理常式。

此方法與 on() 相反。

注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理器,而使用符合此萬用字元的純名稱註冊的處理器將會保留。

另請參閱 on()

public boolean off ( $name, $handler null )
$name 字串

事件名稱

$handler callable|null

要移除的事件處理器。如果為 null,則會移除附加到具名事件的所有處理器。

傳回 boolean

如果找到並分離處理器

                public function off($name, $handler = null)
{
    $this->ensureBehaviors();
    if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
        return false;
    }
    if ($handler === null) {
        unset($this->_events[$name], $this->_eventWildcards[$name]);
        return true;
    }
    $removed = false;
    // plain event names
    if (isset($this->_events[$name])) {
        foreach ($this->_events[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_events[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_events[$name] = array_values($this->_events[$name]);
            return true;
        }
    }
    // wildcard event names
    if (isset($this->_eventWildcards[$name])) {
        foreach ($this->_eventWildcards[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_eventWildcards[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
            // remove empty wildcards to save future redundant regex checks:
            if (empty($this->_eventWildcards[$name])) {
                unset($this->_eventWildcards[$name]);
            }
        }
    }
    return $removed;
}

            
on() public method

Defined in: yii\base\Component::on()

將事件處理常式附加到事件。

事件處理器必須是有效的 PHP 回呼。以下是一些範例

function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()

事件處理器必須使用以下簽章定義:

function ($event)

其中 $event 是一個 yii\base\Event 物件,其中包含與事件關聯的參數。

自 2.0.14 版起,您可以將事件名稱指定為萬用字元模式

$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});

另請參閱 off()

public void on ( $name, $handler, $data null, $append true )
$name 字串

事件名稱

$handler callable

事件處理器

$data 混合

事件觸發時要傳遞給事件處理器的資料。當叫用事件處理器時,可以透過 yii\base\Event::$data 存取此資料。

$append boolean

是否將新的事件處理器附加到現有處理器清單的末尾。如果為 false,則新的處理器將插入到現有處理器清單的開頭。

                public function on($name, $handler, $data = null, $append = true)
{
    $this->ensureBehaviors();
    if (strpos($name, '*') !== false) {
        if ($append || empty($this->_eventWildcards[$name])) {
            $this->_eventWildcards[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_eventWildcards[$name], [$handler, $data]);
        }
        return;
    }
    if ($append || empty($this->_events[$name])) {
        $this->_events[$name][] = [$handler, $data];
    } else {
        array_unshift($this->_events[$name], [$handler, $data]);
    }
}

            
primaryKey() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::primaryKey()

建立主鍵欄位。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
renameColumn() public method

建立並執行用於重新命名欄位的 SQL 陳述式。

public void renameColumn ( $table, $name, $newName )
$table 字串

要重新命名的欄位所屬的表格。名稱將由方法正確地加上引號。

$name 字串

欄位的舊名稱。名稱將由方法正確地加上引號。

$newName 字串

欄位的新名稱。名稱將由方法正確地加上引號。

                public function renameColumn($table, $name, $newName)
{
    $time = $this->beginCommand("rename column $name in table $table to $newName");
    $this->db->createCommand()->renameColumn($table, $name, $newName)->execute();
    $this->endCommand($time);
}

            
renameTable() public method

建立並執行用於重新命名資料庫表格的 SQL 陳述式。

public void renameTable ( $table, $newName )
$table 字串

要重新命名的表格。名稱將由方法正確地加上引號。

$newName 字串

表格的新名稱。名稱將由方法正確地加上引號。

                public function renameTable($table, $newName)
{
    $time = $this->beginCommand("rename table $table to $newName");
    $this->db->createCommand()->renameTable($table, $newName)->execute();
    $this->endCommand($time);
}

            
safeDown() public method

此方法包含移除此遷移時要執行的邏輯。

此方法與 down() 的不同之處在於,此處實作的 DB 邏輯將包含在 DB 交易中。如果 DB 邏輯需要包含在交易中,子類別可以實作此方法來代替 down()

注意:並非所有 DBMS 都支援交易。而且某些 DB 查詢無法放入交易中。如需範例,請參閱 隱含提交

public false|void|mixed safeDown ( )
傳回 false|void|mixed

傳回 false 值以指示遷移失敗且不應繼續進行。所有其他傳回值表示遷移成功。

                public function safeDown()
{
}

            
safeUp() public method

此方法包含套用此遷移時要執行的邏輯。

此方法與 up() 的不同之處在於,此處實作的 DB 邏輯將包含在 DB 交易中。如果 DB 邏輯需要包含在交易中,子類別可以實作此方法來代替 up()

注意:並非所有 DBMS 都支援交易。而且某些 DB 查詢無法放入交易中。如需範例,請參閱 隱含提交

public false|void|mixed safeUp ( )
傳回 false|void|mixed

傳回 false 值以指示遷移失敗且不應繼續進行。所有其他傳回值表示遷移成功。

                public function safeUp()
{
}

            
smallInteger() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::smallInteger()

建立 smallint 欄位。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
string() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::string()

建立字串欄位。

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

欄位大小定義,即最大字串長度。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
text() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::text()

建立 text 欄位。

public yii\db\ColumnSchemaBuilder text ( )
傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
time() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::time()

建立時間欄位。

public yii\db\ColumnSchemaBuilder time ( $precision null )
$precision integer|null

欄位值精確度。傳遞給欄位類型的第一個參數,例如 TIME(precision)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
timestamp() public method (available since version 2.0.6)

Defined in: yii\db\SchemaBuilderTrait::timestamp()

建立時間戳記欄位。

public yii\db\ColumnSchemaBuilder timestamp ( $precision null )
$precision integer|null

欄位值精確度。傳遞給欄位類型的第一個參數,例如 TIMESTAMP(precision)。如果 DBMS 不支援此參數,則會忽略此參數。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
tinyInteger() public method (available since version 2.0.14)

Defined in: yii\db\SchemaBuilderTrait::tinyInteger()

建立 tinyint 欄位。如果 DBMS 不支援 tinyint,將會使用 smallint。

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

欄位大小或精確度定義。如果 DBMS 不支援此參數,則會忽略它。

傳回 yii\db\ColumnSchemaBuilder

可以進一步自訂的欄位實例。

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

            
trigger() public method

Defined in: yii\base\Component::trigger()

觸發事件。

此方法表示事件的發生。它會叫用事件的所有附加處理器,包括類別層級的處理器。

public void trigger ( $name, yii\base\Event $event null )
$name 字串

事件名稱

$event yii\base\Event|null

事件實例。如果未設定,將會建立預設的 yii\base\Event 物件。

                public function trigger($name, Event $event = null)
{
    $this->ensureBehaviors();
    $eventHandlers = [];
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (StringHelper::matchWildcard($wildcard, $name)) {
            $eventHandlers[] = $handlers;
        }
    }
    if (!empty($this->_events[$name])) {
        $eventHandlers[] = $this->_events[$name];
    }
    if (!empty($eventHandlers)) {
        $eventHandlers = call_user_func_array('array_merge', $eventHandlers);
        if ($event === null) {
            $event = new Event();
        }
        if ($event->sender === null) {
            $event->sender = $this;
        }
        $event->handled = false;
        $event->name = $name;
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            // stop further handling if the event is handled
            if ($event->handled) {
                return;
            }
        }
    }
    // invoke class-level attached handlers
    Event::trigger($this, $name, $event);
}

            
truncateTable() public method

建立並執行用於截斷資料庫表格的 SQL 陳述式。

public void truncateTable ( $table )
$table 字串

要截斷的表格。名稱將由方法正確地加上引號。

                public function truncateTable($table)
{
    $time = $this->beginCommand("truncate table $table");
    $this->db->createCommand()->truncateTable($table)->execute();
    $this->endCommand($time);
}

            
up() public method

此方法包含套用此遷移時要執行的邏輯。

子類別可以覆寫此方法以提供實際的遷移邏輯。

public false|void|mixed up ( )
傳回 false|void|mixed

傳回 false 值以指示遷移失敗且不應繼續進行。所有其他傳回值表示遷移成功。

                public function up()
{
    $transaction = $this->db->beginTransaction();
    try {
        if ($this->safeUp() === false) {
            $transaction->rollBack();
            return false;
        }
        $transaction->commit();
    } catch (\Exception $e) {
        $this->printException($e);
        $transaction->rollBack();
        return false;
    } catch (\Throwable $e) {
        $this->printException($e);
        $transaction->rollBack();
        return false;
    }
    return null;
}

            
update() public method

建立並執行 UPDATE SQL 陳述式。

此方法將正確地跳脫欄位名稱,並繫結要更新的值。

public void update ( $table, $columns, $condition '', $params = [] )
$table 字串

要更新的表格。

$columns 陣列

要更新的欄位資料 (名稱 => 值)。

$condition array|string

將放入 WHERE 部分的條件。請參閱 yii\db\Query::where(),了解如何指定條件。

$params 陣列

要綁定到查詢的參數。

                public function update($table, $columns, $condition = '', $params = [])
{
    $time = $this->beginCommand("update $table");
    $this->db->createCommand()->update($table, $columns, $condition, $params)->execute();
    $this->endCommand($time);
}

            
upsert() public method (available since version 2.0.14)

建立並執行命令,以在資料庫表格中插入列(如果它們尚不存在,符合唯一約束),或者在它們存在時更新它們。

此方法將正確地逸出欄位名稱,並繫結要插入的值。

public void upsert ( $table, $insertColumns, $updateColumns true, $params = [] )
$table 字串

新資料列將插入/更新至的表格。

$insertColumns array|yii\db\Query

要插入表格的欄位資料 (名稱 => 值) 或 yii\db\Query 的實例,以執行 INSERT INTO ... SELECT SQL 陳述式。

$updateColumns array|boolean

如果欄位資料已存在,則要更新的欄位資料 (名稱 => 值)。如果傳遞 true,欄位資料將更新為符合插入的欄位資料。如果傳遞 false,如果欄位資料已存在,則不會執行更新。

$params 陣列

要繫結到命令的參數。

                public function upsert($table, $insertColumns, $updateColumns = true, $params = [])
{
    $time = $this->beginCommand("upsert into $table");
    $this->db->createCommand()->upsert($table, $insertColumns, $updateColumns, $params)->execute();
    $this->endCommand($time);
}