0 追蹤者

類別 yii\db\oci\QueryBuilder

繼承yii\db\oci\QueryBuilder » yii\db\QueryBuilder » yii\base\BaseObject
實作yii\base\Configurable
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/db/oci/QueryBuilder.php

QueryBuilder 是 Oracle 資料庫的查詢建構器。

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$conditionBuilders 陣列 查詢條件到建構器方法的映射。 yii\db\QueryBuilder
$conditionClasses 陣列 條件別名到條件類別的映射。 yii\db\QueryBuilder
$db yii\db\Connection 資料庫連線。 yii\db\QueryBuilder
$expressionBuilders string[]|yii\db\ExpressionBuilderInterface[] 將表達式類別映射到表達式建構器類別。 yii\db\QueryBuilder
$separator 字串 SQL 陳述式不同片段之間的分隔符。 yii\db\QueryBuilder
$typeMap 陣列 從抽象欄位類型(鍵)到實體欄位類型(值)的映射。 yii\db\oci\QueryBuilder

受保護的屬性

隱藏繼承的屬性

屬性 類型 描述 定義於

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫非類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子。 yii\db\QueryBuilder
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
addCheck() 建立 SQL 命令,用於將檢查約束新增到現有表格。 yii\db\QueryBuilder
addColumn() 建構 SQL 陳述式,用於新增新的資料庫欄位。 yii\db\QueryBuilder
addCommentOnColumn() 建構 SQL 命令,用於為欄位新增註解。 yii\db\QueryBuilder
addCommentOnTable() 建構 SQL 命令,用於為表格新增註解。 yii\db\QueryBuilder
addDefaultValue() 建立 SQL 命令,用於將預設值約束新增到現有表格。 yii\db\QueryBuilder
addForeignKey() 建構 SQL 陳述式,用於將外來鍵約束新增到現有表格。 yii\db\oci\QueryBuilder
addPrimaryKey() 建構 SQL 陳述式,用於將主鍵約束新增到現有表格。 yii\db\QueryBuilder
addUnique() 建立 SQL 命令,用於將唯一約束新增到現有表格。 yii\db\QueryBuilder
alterColumn() 建構 SQL 陳述式,用於變更欄位的定義。 yii\db\oci\QueryBuilder
batchInsert() 產生批次 INSERT SQL 陳述式。 yii\db\oci\QueryBuilder
bindParam() 輔助方法,使用 PARAM_PREFIX 將 $value 新增到 $params 陣列。 yii\db\QueryBuilder
build() yii\db\Query 物件產生 SELECT SQL 陳述式。 yii\db\QueryBuilder
buildAndCondition() 使用 ANDOR 運算子連接兩個或多個 SQL 表達式。 yii\db\QueryBuilder
buildBetweenCondition() 使用 BETWEEN 運算子建立 SQL 表達式。 yii\db\QueryBuilder
buildColumns() 處理欄位,並在必要時正確地加上引號。 yii\db\QueryBuilder
buildCondition() 解析條件規格,並產生對應的 SQL 表達式。 yii\db\QueryBuilder
buildExistsCondition() 使用 EXISTS 運算子建立 SQL 表達式。 yii\db\QueryBuilder
buildExpression() 建構給定的 $expression yii\db\QueryBuilder
buildFrom() yii\db\QueryBuilder
buildGroupBy() yii\db\QueryBuilder
buildHashCondition() 根據欄位-值對建立條件。 yii\db\QueryBuilder
buildHaving() yii\db\QueryBuilder
buildInCondition() 使用 IN 運算子建立 SQL 表達式。 yii\db\QueryBuilder
buildJoin() yii\db\QueryBuilder
buildLikeCondition() 使用 LIKE 運算子建立 SQL 表達式。 yii\db\QueryBuilder
buildLimit() yii\db\QueryBuilder
buildNotCondition() 使用 NOT 運算子反轉 SQL 表達式。 yii\db\QueryBuilder
buildOrderBy() yii\db\QueryBuilder
buildOrderByAndLimit() 建構 ORDER BY 和 LIMIT/OFFSET 子句,並將它們附加到給定的 SQL。 yii\db\oci\QueryBuilder
buildSelect() yii\db\QueryBuilder
buildSimpleCondition() 建立類似 "column" operator value 的 SQL 表達式。 yii\db\QueryBuilder
buildUnion() yii\db\QueryBuilder
buildWhere() yii\db\QueryBuilder
buildWithQueries() yii\db\QueryBuilder
canGetProperty() 傳回值,指示是否可以讀取屬性。 yii\base\BaseObject
canSetProperty() 傳回值,指示是否可以設定屬性。 yii\base\BaseObject
checkIntegrity() 建構 SQL 陳述式,用於啟用或停用完整性檢查。 yii\db\QueryBuilder
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
createConditionFromArray() 根據 $conditionClasses 映射,將陣列格式定義的 $condition(如 yii\db\Query::where() 中所述)轉換為 yii\db\condition\ConditionInterface 的實例。 yii\db\QueryBuilder
createIndex() 建構 SQL 陳述式,用於建立新的索引。 yii\db\QueryBuilder
createTable() 建構 SQL 陳述式,用於建立新的資料庫表格。 yii\db\QueryBuilder
createView() 建立 SQL 視圖。 yii\db\QueryBuilder
delete() 建立 DELETE SQL 陳述式。 yii\db\QueryBuilder
dropCheck() 建立 SQL 命令,用於刪除檢查約束。 yii\db\QueryBuilder
dropColumn() 建構 SQL 陳述式,用於刪除資料庫欄位。 yii\db\QueryBuilder
dropCommentFromColumn() 建構 SQL 命令,用於為欄位新增註解。 yii\db\oci\QueryBuilder
dropCommentFromTable() 建構 SQL 命令,用於為表格新增註解。 yii\db\oci\QueryBuilder
dropDefaultValue() 建立 SQL 命令,用於刪除預設值約束。 yii\db\QueryBuilder
dropForeignKey() 建構 SQL 陳述式,用於刪除外來鍵約束。 yii\db\QueryBuilder
dropIndex() 建構 SQL 陳述式,用於刪除索引。 yii\db\oci\QueryBuilder
dropPrimaryKey() 建構 SQL 陳述式,用於從現有表格移除主鍵約束。 yii\db\QueryBuilder
dropTable() 建構 SQL 陳述式,用於刪除資料庫表格。 yii\db\QueryBuilder
dropUnique() 建立 SQL 命令,用於刪除唯一約束。 yii\db\QueryBuilder
dropView() 刪除 SQL 視圖。 yii\db\QueryBuilder
executeResetSequence() 執行 SQL 陳述式,用於重設表格主鍵的序列值。 yii\db\oci\QueryBuilder
getColumnType() 將抽象欄位類型轉換為實體欄位類型。 yii\db\QueryBuilder
getExpressionBuilder() 取得適用於 $expression 的 yii\db\ExpressionBuilderInterface 物件。 yii\db\QueryBuilder
hasMethod() 傳回值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\db\QueryBuilder
insert() 建立 INSERT SQL 陳述式。 yii\db\QueryBuilder
renameColumn() 建構 SQL 陳述式,用於重新命名欄位。 yii\db\QueryBuilder
renameTable() 建構 SQL 陳述式,用於重新命名資料庫表格。 yii\db\oci\QueryBuilder
resetSequence() 建立 SQL 陳述式,用於重設表格主鍵的序列值。 yii\db\QueryBuilder
selectExists() 建立 SELECT EXISTS() SQL 陳述式。 yii\db\oci\QueryBuilder
setConditionClasses() $conditionClasses 屬性的設定器。 yii\db\QueryBuilder
setExpressionBuilders() $expressionBuilders 屬性的設定器。 yii\db\QueryBuilder
truncateTable() 建構 SQL 陳述式,用於截斷資料庫表格。 yii\db\QueryBuilder
update() 建立 UPDATE SQL 陳述式。 yii\db\QueryBuilder
upsert() 建立 SQL 陳述式,用於在資料庫表格中插入資料列(如果它們尚不存在,則比對唯一約束),或在它們已存在時更新它們。 yii\db\oci\QueryBuilder

受保護的方法

隱藏繼承的方法

方法 描述 定義於
defaultConditionClasses() 包含預設條件類別的陣列。如果您想變更查詢建構器的預設條件類別,請擴展此方法。請參閱 $conditionClasses 文件以取得詳細資訊。 yii\db\QueryBuilder
defaultExpressionBuilders() 包含預設表達式建構器的陣列。如果您想變更此查詢建構器的預設表達式建構器,請擴展並覆寫此方法。請參閱 $expressionBuilders 文件以取得詳細資訊。 yii\db\oci\QueryBuilder
extractAlias() 如果存在表格別名,則提取表格別名,否則傳回 false yii\db\QueryBuilder
hasLimit() 檢查給定的限制是否有效。 yii\db\QueryBuilder
hasOffset() 檢查給定的偏移量是否有效。 yii\db\QueryBuilder
prepareInsertSelectSubQuery() 為 INSERT INTO ... SELECT SQL 陳述式準備 select-subquery 和欄位名稱。 yii\db\QueryBuilder
prepareInsertValues() INSERT SQL 陳述式準備 VALUES 部分。 yii\db\oci\QueryBuilder
prepareUpdateSets() UPDATE SQL 陳述式準備 SET 部分。 yii\db\QueryBuilder
prepareUpsertColumns() yii\db\QueryBuilder

常數

隱藏繼承的常數

常數 描述 定義於
PARAM_PREFIX ':qp' 自動產生的查詢綁定參數的前綴。 yii\db\QueryBuilder

屬性詳細資訊

隱藏繼承的屬性

$typeMap 公共屬性

從抽象欄位類型(鍵)到實體欄位類型(值)的映射。

public array $typeMap = [
    \
yii\db\oci\Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY',
    \
yii\db\oci\Schema::TYPE_UPK => 'NUMBER(10) UNSIGNED NOT NULL PRIMARY KEY',
    \
yii\db\oci\Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY',
    \
yii\db\oci\Schema::TYPE_UBIGPK => 'NUMBER(20) UNSIGNED NOT NULL PRIMARY KEY',
    \
yii\db\oci\Schema::TYPE_CHAR => 'CHAR(1)',
    \
yii\db\oci\Schema::TYPE_STRING => 'VARCHAR2(255)',
    \
yii\db\oci\Schema::TYPE_TEXT => 'CLOB',
    \
yii\db\oci\Schema::TYPE_TINYINT => 'NUMBER(3)',
    \
yii\db\oci\Schema::TYPE_SMALLINT => 'NUMBER(5)',
    \
yii\db\oci\Schema::TYPE_INTEGER => 'NUMBER(10)',
    \
yii\db\oci\Schema::TYPE_BIGINT => 'NUMBER(20)',
    \
yii\db\oci\Schema::TYPE_FLOAT => 'NUMBER',
    \
yii\db\oci\Schema::TYPE_DOUBLE => 'NUMBER',
    \
yii\db\oci\Schema::TYPE_DECIMAL => 'NUMBER',
    \
yii\db\oci\Schema::TYPE_DATETIME => 'TIMESTAMP',
    \
yii\db\oci\Schema::TYPE_TIMESTAMP => 'TIMESTAMP',
    \
yii\db\oci\Schema::TYPE_TIME => 'TIMESTAMP',
    \
yii\db\oci\Schema::TYPE_DATE => 'DATE',
    \
yii\db\oci\Schema::TYPE_BINARY => 'BLOB',
    \
yii\db\oci\Schema::TYPE_BOOLEAN => 'NUMBER(1)',
    \
yii\db\oci\Schema::TYPE_MONEY => 'NUMBER(19,4)',
]

方法詳細資訊

隱藏繼承的方法

__call() 公共方法

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

呼叫非類別方法的具名方法。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,當調用未知方法時,它會被隱式呼叫。

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

方法名稱

$params 陣列

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() 公共方法

定義於: yii\db\QueryBuilder::__construct()

建構子。

public void __construct ( $connection, $config = [] )
$connection yii\db\Connection

資料庫連線。

$config 陣列

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

                public function __construct($connection, $config = [])
{
    $this->db = $connection;
    parent::__construct($config);
}

            
__get() 公共方法

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

傳回物件屬性的值。

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

另請參閱 __set()

public mixed __get ( $name )
$name 字串

屬性名稱

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

            
__isset() 公共方法

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

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

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

請注意,如果未定義屬性,將傳回 false。

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

public boolean __isset ( $name )
$name 字串

屬性名稱或事件名稱

return boolean

具名屬性是否已設定(非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() 公共方法

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

設定物件屬性的值。

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

另請參閱 __get()

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

屬性名稱或事件名稱

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

            
__unset() 公共方法

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

將物件屬性設定為 null。

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

請注意,如果未定義屬性,此方法將不執行任何操作。如果屬性為唯讀,它將拋出例外。

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

public void __unset ( $name )
$name 字串

屬性名稱

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

            
addCheck() 公共方法 (自版本 2.0.13 起可用)

定義於: yii\db\QueryBuilder::addCheck()

建立 SQL 命令,用於將檢查約束新增到現有表格。

public string addCheck ( $name, $table, $expression )
$name 字串

檢查約束的名稱。名稱將由此方法正確地加上引號。

$table 字串

將新增檢查約束的表格。名稱將由此方法正確地加上引號。

$expression 字串

CHECK 約束的 SQL。

return 字串

用於將檢查約束新增到現有表格的 SQL 陳述式。

                public function addCheck($name, $table, $expression)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD CONSTRAINT '
        . $this->db->quoteColumnName($name) . ' CHECK (' . $this->db->quoteSql($expression) . ')';
}

            
addColumn() 公共方法

定義於: yii\db\QueryBuilder::addColumn()

建構 SQL 陳述式,用於新增新的資料庫欄位。

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

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

$column 字串

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

$type 字串

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

return 字串

用於新增新欄位的 SQL 陳述式。

                public function addColumn($table, $column, $type)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' ADD ' . $this->db->quoteColumnName($column) . ' '
        . $this->getColumnType($type);
}

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

定義於: yii\db\QueryBuilder::addCommentOnColumn()

建構 SQL 命令,用於為欄位新增註解。

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

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

$column 字串

要註解的欄位的名稱。欄位名稱將由此方法正確地加上引號。

$comment 字串

要新增的註解文字。註解將由此方法正確地加上引號。

return 字串

用於為欄位新增註解的 SQL 陳述式

                public function addCommentOnColumn($table, $column, $comment)
{
    return 'COMMENT ON COLUMN ' . $this->db->quoteTableName($table) . '.' . $this->db->quoteColumnName($column) . ' IS ' . $this->db->quoteValue($comment);
}

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

定義於: yii\db\QueryBuilder::addCommentOnTable()

建構 SQL 命令,用於為表格新增註解。

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

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

$comment 字串

要新增的註解文字。註解將由此方法正確地加上引號。

return 字串

用於為表格新增註解的 SQL 陳述式

                public function addCommentOnTable($table, $comment)
{
    return 'COMMENT ON TABLE ' . $this->db->quoteTableName($table) . ' IS ' . $this->db->quoteValue($comment);
}

            
addDefaultValue() 公共方法 (自版本 2.0.13 起可用)

定義於: yii\db\QueryBuilder::addDefaultValue()

建立 SQL 命令,用於將預設值約束新增到現有表格。

public string addDefaultValue ( $name, $table, $column, $value )
$name 字串

預設值約束的名稱。名稱將由該方法正確地加上引號。

$table 字串

將加入預設值約束的表格。名稱將由該方法正確地加上引號。

$column 字串

將在其上加入約束的欄位名稱。名稱將由該方法正確地加上引號。

$value mixed

預設值。

return 字串

用於將預設值約束加入現有表格的 SQL 語句。

throws yii\base\NotSupportedException

如果底層 DBMS 不支援此功能。

                public function addDefaultValue($name, $table, $column, $value)
{
    throw new NotSupportedException($this->db->getDriverName() . ' does not support adding default value constraints.');
}

            
addForeignKey() public method

建構 SQL 陳述式,用於將外來鍵約束新增到現有表格。

此方法將正確地為表格和欄位名稱加上引號。

public string 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

return 字串

用於將外鍵約束加入現有表格的 SQL 語句。

                public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
    $sql = 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' ADD CONSTRAINT ' . $this->db->quoteColumnName($name)
        . ' FOREIGN KEY (' . $this->buildColumns($columns) . ')'
        . ' REFERENCES ' . $this->db->quoteTableName($refTable)
        . ' (' . $this->buildColumns($refColumns) . ')';
    if ($delete !== null) {
        $sql .= ' ON DELETE ' . $delete;
    }
    if ($update !== null) {
        throw new Exception('Oracle does not support ON UPDATE clause.');
    }
    return $sql;
}

            
addPrimaryKey() public method

定義於: yii\db\QueryBuilder::addPrimaryKey()

建構 SQL 陳述式,用於將主鍵約束新增到現有表格。

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

主鍵約束的名稱。

$table 字串

將加入主鍵約束的表格。

$columns string|array

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

return 字串

用於將主鍵約束加入現有表格的 SQL 語句。

                public function addPrimaryKey($name, $table, $columns)
{
    if (is_string($columns)) {
        $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
    }
    foreach ($columns as $i => $col) {
        $columns[$i] = $this->db->quoteColumnName($col);
    }
    return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD CONSTRAINT '
        . $this->db->quoteColumnName($name) . ' PRIMARY KEY ('
        . implode(', ', $columns) . ')';
}

            
addUnique() public method (自 2.0.13 版本起可用)

定義於: yii\db\QueryBuilder::addUnique()

建立 SQL 命令,用於將唯一約束新增到現有表格。

public string addUnique ( $name, $table, $columns )
$name 字串

唯一性約束的名稱。名稱將由該方法正確地加上引號。

$table 字串

將加入唯一性約束的表格。名稱將由該方法正確地加上引號。

$columns string|array

將在其上加入約束的欄位名稱。如果有多個欄位,請用逗號分隔。名稱將由該方法正確地加上引號。

return 字串

用於將唯一性約束加入現有表格的 SQL 語句。

                public function addUnique($name, $table, $columns)
{
    if (is_string($columns)) {
        $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
    }
    foreach ($columns as $i => $col) {
        $columns[$i] = $this->db->quoteColumnName($col);
    }
    return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD CONSTRAINT '
        . $this->db->quoteColumnName($name) . ' UNIQUE ('
        . implode(', ', $columns) . ')';
}

            
alterColumn() public method

建構 SQL 陳述式,用於變更欄位的定義。

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

要變更欄位的表格。表格名稱將由該方法正確地加上引號。

$column 字串

要變更的欄位名稱。名稱將由該方法正確地加上引號。

$type 字串

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

return 字串

用於變更欄位定義的 SQL 語句。

                public function alterColumn($table, $column, $type)
{
    $type = $this->getColumnType($type);
    return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' MODIFY ' . $this->db->quoteColumnName($column) . ' ' . $this->getColumnType($type);
}

            
batchInsert() public method

產生批次 INSERT SQL 陳述式。

例如,

$sql = $queryBuilder->batchInsert('user', ['name', 'age'], [
    ['Tom', 30],
    ['Jane', 20],
    ['Linda', 25],
]);

請注意,每行中的值必須與對應的欄位名稱匹配。

public string batchInsert ( $table, $columns, $rows, &$params = [] )
$table 字串

將在其中插入新列的表格。

$columns 陣列

欄位名稱

$rows array|Generator

要批量插入表格中的列

$params
return 字串

批量 INSERT SQL 語句

                public function batchInsert($table, $columns, $rows, &$params = [])
{
    if (empty($rows)) {
        return '';
    }
    $schema = $this->db->getSchema();
    if (($tableSchema = $schema->getTableSchema($table)) !== null) {
        $columnSchemas = $tableSchema->columns;
    } else {
        $columnSchemas = [];
    }
    $values = [];
    foreach ($rows as $row) {
        $vs = [];
        foreach ($row as $i => $value) {
            if (isset($columns[$i], $columnSchemas[$columns[$i]])) {
                $value = $columnSchemas[$columns[$i]]->dbTypecast($value);
            }
            if (is_string($value)) {
                $value = $schema->quoteValue($value);
            } elseif (is_float($value)) {
                // ensure type cast always has . as decimal separator in all locales
                $value = StringHelper::floatToString($value);
            } elseif ($value === false) {
                $value = 0;
            } elseif ($value === null) {
                $value = 'NULL';
            } elseif ($value instanceof ExpressionInterface) {
                $value = $this->buildExpression($value, $params);
            }
            $vs[] = $value;
        }
        $values[] = '(' . implode(', ', $vs) . ')';
    }
    if (empty($values)) {
        return '';
    }
    foreach ($columns as $i => $name) {
        $columns[$i] = $schema->quoteColumnName($name);
    }
    $tableAndColumns = ' INTO ' . $schema->quoteTableName($table)
    . ' (' . implode(', ', $columns) . ') VALUES ';
    return 'INSERT ALL ' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL';
}

            
bindParam() public method (自 2.0.14 版本起可用)

定義於: yii\db\QueryBuilder::bindParam()

輔助方法,使用 PARAM_PREFIX 將 $value 新增到 $params 陣列。

public string bindParam ( $value, &$params )
$value string|null
$params 陣列

以參考方式傳遞

return 字串

$params 陣列中的佔位符名稱

                public function bindParam($value, &$params)
{
    $phName = self::PARAM_PREFIX . count($params);
    $params[$phName] = $value;
    return $phName;
}

            
build() public method

定義於: yii\db\QueryBuilder::build()

yii\db\Query 物件產生 SELECT SQL 陳述式。

public array build ( $query, $params = [] )
$query yii\db\Query

將從其生成 SQL 語句的 yii\db\Query 物件。

$params 陣列

要綁定到生成的 SQL 語句的參數。這些參數將與查詢建構過程中生成的其他參數一起包含在結果中。

return 陣列

生成的 SQL 語句(第一個陣列元素)和要綁定到 SQL 語句的相應參數(第二個陣列元素)。傳回的參數包括在 $params 中提供的參數。

                public function build($query, $params = [])
{
    $query = $query->prepare($this);
    $params = empty($params) ? $query->params : array_merge($params, $query->params);
    $clauses = [
        $this->buildSelect($query->select, $params, $query->distinct, $query->selectOption),
        $this->buildFrom($query->from, $params),
        $this->buildJoin($query->join, $params),
        $this->buildWhere($query->where, $params),
        $this->buildGroupBy($query->groupBy),
        $this->buildHaving($query->having, $params),
    ];
    $sql = implode($this->separator, array_filter($clauses));
    $sql = $this->buildOrderByAndLimit($sql, $query->orderBy, $query->limit, $query->offset);
    if (!empty($query->orderBy)) {
        foreach ($query->orderBy as $expression) {
            if ($expression instanceof ExpressionInterface) {
                $this->buildExpression($expression, $params);
            }
        }
    }
    if (!empty($query->groupBy)) {
        foreach ($query->groupBy as $expression) {
            if ($expression instanceof ExpressionInterface) {
                $this->buildExpression($expression, $params);
            }
        }
    }
    $union = $this->buildUnion($query->union, $params);
    if ($union !== '') {
        $sql = "($sql){$this->separator}$union";
    }
    $with = $this->buildWithQueries($query->withQueries, $params);
    if ($with !== '') {
        $sql = "$with{$this->separator}$sql";
    }
    return [$sql, $params];
}

            
buildAndCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildAndCondition()

使用 ANDOR 運算子連接兩個或多個 SQL 表達式。

public string buildAndCondition ( $operator, $operands, &$params )
$operator 字串

用於連接給定運算元的運算符

$operands 陣列

要連接的 SQL 表達式。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

                public function buildAndCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildBetweenCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildBetweenCondition()

使用 BETWEEN 運算子建立 SQL 表達式。

public string buildBetweenCondition ( $operator, $operands, &$params )
$operator 字串

要使用的運算符(例如 BETWEENNOT BETWEEN

$operands 陣列

第一個運算元是欄位名稱。第二個和第三個運算元描述欄位值應在的區間。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

throws yii\base\InvalidArgumentException

如果給定的運算元數量錯誤。

                public function buildBetweenCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildColumns() public method

定義於: yii\db\QueryBuilder::buildColumns()

處理欄位,並在必要時正確地加上引號。

它將所有欄位與逗號作為分隔符連接成一個字串。

public string buildColumns ( $columns )
$columns string|array

要處理的欄位

return 字串

處理結果

                public function buildColumns($columns)
{
    if (!is_array($columns)) {
        if (strpos($columns, '(') !== false) {
            return $columns;
        }
        $rawColumns = $columns;
        $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
        if ($columns === false) {
            throw new InvalidArgumentException("$rawColumns is not valid columns.");
        }
    }
    foreach ($columns as $i => $column) {
        if ($column instanceof ExpressionInterface) {
            $columns[$i] = $this->buildExpression($column);
        } elseif (strpos($column, '(') === false) {
            $columns[$i] = $this->db->quoteColumnName($column);
        }
    }
    return implode(', ', $columns);
}

            
buildCondition() public method

定義於: yii\db\QueryBuilder::buildCondition()

解析條件規格,並產生對應的 SQL 表達式。

public string buildCondition ( $condition, &$params )
$condition string|array|yii\db\ExpressionInterface

條件規範。請參考 yii\db\Query::where() 以了解如何指定條件。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

                public function buildCondition($condition, &$params)
{
    if (is_array($condition)) {
        if (empty($condition)) {
            return '';
        }
        $condition = $this->createConditionFromArray($condition);
    }
    if ($condition instanceof ExpressionInterface) {
        return $this->buildExpression($condition, $params);
    }
    return (string)$condition;
}

            
buildExistsCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildExistsCondition()

使用 EXISTS 運算子建立 SQL 表達式。

public string buildExistsCondition ( $operator, $operands, &$params )
$operator 字串

要使用的運算符(例如 EXISTSNOT EXISTS

$operands 陣列

僅包含一個元素,即表示子查詢的 yii\db\Query 物件。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

throws yii\base\InvalidArgumentException

如果運算元不是 yii\db\Query 物件。

                public function buildExistsCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildExpression() public method (自 2.0.14 版本起可用)
public string buildExpression ( yii\db\ExpressionInterface $expression, &$params = [] )
$expression yii\db\ExpressionInterface

要建構的表達式

$params 陣列

要綁定到生成的 SQL 語句的參數。這些參數將與表達式建構過程中生成的其他參數一起包含在結果中。

return 字串

在傳遞到 DBMS 之前,既不會加上引號也不會編碼的 SQL 語句

throws yii\base\InvalidArgumentException

當此 QueryBuilder 不支援 $expression 建構時。

                public function buildExpression(ExpressionInterface $expression, &$params = [])
{
    $builder = $this->getExpressionBuilder($expression);
    return $builder->build($expression, $params);
}

            
buildFrom() public method
public string buildFrom ( $tables, &$params )
$tables 陣列
$params 陣列

要填充的綁定參數

return 字串

yii\db\Query::$from 建構的 FROM 子句。

                public function buildFrom($tables, &$params)
{
    if (empty($tables)) {
        return '';
    }
    $tables = $this->quoteTableNames($tables, $params);
    return 'FROM ' . implode(', ', $tables);
}

            
buildGroupBy() public method
public string buildGroupBy ( $columns )
$columns 陣列
return 字串

GROUP BY 子句

                public function buildGroupBy($columns)
{
    if (empty($columns)) {
        return '';
    }
    foreach ($columns as $i => $column) {
        if ($column instanceof ExpressionInterface) {
            $columns[$i] = $this->buildExpression($column);
        } elseif (strpos($column, '(') === false) {
            $columns[$i] = $this->db->quoteColumnName($column);
        }
    }
    return 'GROUP BY ' . implode(', ', $columns);
}

            
buildHashCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildHashCondition()

根據欄位-值對建立條件。

public string buildHashCondition ( $condition, &$params )
$condition 陣列

條件規範。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

                public function buildHashCondition($condition, &$params)
{
    return $this->buildCondition(new HashCondition($condition), $params);
}

            
buildHaving() public method
public string buildHaving ( $condition, &$params )
$condition string|array
$params 陣列

要填充的綁定參數

return 字串

yii\db\Query::$having 建構的 HAVING 子句。

                public function buildHaving($condition, &$params)
{
    $having = $this->buildCondition($condition, $params);
    return $having === '' ? '' : 'HAVING ' . $having;
}

            
buildInCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildInCondition()

使用 IN 運算子建立 SQL 表達式。

public string buildInCondition ( $operator, $operands, &$params )
$operator 字串

要使用的運算符(例如 INNOT IN

$operands 陣列

第一個運算元是欄位名稱。如果它是一個陣列,將生成一個複合 IN 條件。第二個運算元是欄位值應在其中的值陣列。如果它是一個空陣列,則如果運算符為 IN,則生成的表達式將為 false 值,如果運算符為 NOT IN,則為空。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

throws yii\db\Exception

如果給定的運算元數量錯誤。

                public function buildInCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildJoin() public method
public string buildJoin ( $joins, &$params )
$joins 陣列
$params 陣列

要填充的綁定參數

return 字串

yii\db\Query::$join 建構的 JOIN 子句。

throws yii\db\Exception

如果 $joins 參數格式不正確

                public function buildJoin($joins, &$params)
{
    if (empty($joins)) {
        return '';
    }
    foreach ($joins as $i => $join) {
        if (!is_array($join) || !isset($join[0], $join[1])) {
            throw new Exception('A join clause must be specified as an array of join type, join table, and optionally join condition.');
        }
        // 0:join type, 1:join table, 2:on-condition (optional)
        list($joinType, $table) = $join;
        $tables = $this->quoteTableNames((array)$table, $params);
        $table = reset($tables);
        $joins[$i] = "$joinType $table";
        if (isset($join[2])) {
            $condition = $this->buildCondition($join[2], $params);
            if ($condition !== '') {
                $joins[$i] .= ' ON ' . $condition;
            }
        }
    }
    return implode($this->separator, $joins);
}

            
buildLikeCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildLikeCondition()

使用 LIKE 運算子建立 SQL 表達式。

public string buildLikeCondition ( $operator, $operands, &$params )
$operator 字串

要使用的運算符(例如 LIKENOT LIKEOR LIKEOR NOT LIKE

$operands 陣列

兩個或三個運算元的陣列

  • 第一個運算元是欄位名稱。
  • 第二個運算元是單個值或應與欄位值比較的數值陣列。如果它是一個空陣列,則如果運算符為 LIKEOR LIKE,則生成的表達式將為 false 值,如果運算符為 NOT LIKEOR NOT LIKE,則為空。
  • 還可以提供可選的第三個運算元,以指定如何轉義值中的特殊字元。運算元應為從特殊字元到其轉義對應項的映射陣列。如果未提供此運算元,則將使用預設轉義映射。您可以使用 false 或空陣列來指示值已轉義,並且不應應用轉義。請注意,當使用轉義映射時(或未提供第三個運算元),值將自動包含在一對百分號字元中。
$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

throws yii\base\InvalidArgumentException

如果給定的運算元數量錯誤。

                public function buildLikeCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildLimit() public method
public string buildLimit ( $limit, $offset )
$limit integer
$offset integer
return 字串

LIMIT 和 OFFSET 子句

                public function buildLimit($limit, $offset)
{
    $sql = '';
    if ($this->hasLimit($limit)) {
        $sql = 'LIMIT ' . $limit;
    }
    if ($this->hasOffset($offset)) {
        $sql .= ' OFFSET ' . $offset;
    }
    return ltrim($sql);
}

            
buildNotCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

定義於: yii\db\QueryBuilder::buildNotCondition()

使用 NOT 運算子反轉 SQL 表達式。

public string buildNotCondition ( $operator, $operands, &$params )
$operator 字串

用於連接給定運算元的運算符

$operands 陣列

要連接的 SQL 表達式。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

throws yii\base\InvalidArgumentException

如果給定的運算元數量錯誤。

                public function buildNotCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildOrderBy() public method
public string buildOrderBy ( $columns )
$columns 陣列
return 字串

yii\db\Query::$orderBy 建構的 ORDER BY 子句。

                public function buildOrderBy($columns)
{
    if (empty($columns)) {
        return '';
    }
    $orders = [];
    foreach ($columns as $name => $direction) {
        if ($direction instanceof ExpressionInterface) {
            $orders[] = $this->buildExpression($direction);
        } else {
            $orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : '');
        }
    }
    return 'ORDER BY ' . implode(', ', $orders);
}

            
buildOrderByAndLimit() public method

建構 ORDER BY 和 LIMIT/OFFSET 子句,並將它們附加到給定的 SQL。

public string buildOrderByAndLimit ( $sql, $orderBy, $limit, $offset )
$sql 字串

現有的 SQL (不含 ORDER BY/LIMIT/OFFSET)

$orderBy 陣列

排序依據的欄位。有關如何指定此參數的更多詳細資訊,請參閱 yii\db\Query::orderBy()

$limit integer

限制數量。有關更多詳細資訊,請參閱 yii\db\Query::limit()

$offset integer

偏移量。有關更多詳細資訊,請參閱 yii\db\Query::offset()

return 字串

完成 ORDER BY/LIMIT/OFFSET 的 SQL (如果有的話)

                public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset)
{
    $orderBy = $this->buildOrderBy($orderBy);
    if ($orderBy !== '') {
        $sql .= $this->separator . $orderBy;
    }
    $filters = [];
    if ($this->hasOffset($offset)) {
        $filters[] = 'rowNumId > ' . $offset;
    }
    if ($this->hasLimit($limit)) {
        $filters[] = 'rownum <= ' . $limit;
    }
    if (empty($filters)) {
        return $sql;
    }
    $filter = implode(' AND ', $filters);
    return <<<EOD
 USER_SQL AS ($sql),
PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
CT *
 PAGINATION
E $filter

}

            
buildSelect() public method
public string buildSelect ( $columns, &$params, $distinct false, $selectOption null )
$columns 陣列
$params 陣列

要填充的綁定參數

$distinct boolean
$selectOption string|null
return 字串

yii\db\Query::$select 建構的 SELECT 子句。

                public function buildSelect($columns, &$params, $distinct = false, $selectOption = null)
{
    $select = $distinct ? 'SELECT DISTINCT' : 'SELECT';
    if ($selectOption !== null) {
        $select .= ' ' . $selectOption;
    }
    if (empty($columns)) {
        return $select . ' *';
    }
    foreach ($columns as $i => $column) {
        if ($column instanceof ExpressionInterface) {
            if (is_int($i)) {
                $columns[$i] = $this->buildExpression($column, $params);
            } else {
                $columns[$i] = $this->buildExpression($column, $params) . ' AS ' . $this->db->quoteColumnName($i);
            }
        } elseif ($column instanceof Query) {
            list($sql, $params) = $this->build($column, $params);
            $columns[$i] = "($sql) AS " . $this->db->quoteColumnName($i);
        } elseif (is_string($i) && $i !== $column) {
            if (strpos($column, '(') === false) {
                $column = $this->db->quoteColumnName($column);
            }
            $columns[$i] = "$column AS " . $this->db->quoteColumnName($i);
        } elseif (strpos($column, '(') === false) {
            if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_\.]+)$/', $column, $matches)) {
                $columns[$i] = $this->db->quoteColumnName($matches[1]) . ' AS ' . $this->db->quoteColumnName($matches[2]);
            } else {
                $columns[$i] = $this->db->quoteColumnName($column);
            }
        }
    }
    return $select . ' ' . implode(', ', $columns);
}

            
buildSimpleCondition() public method
自 2.0.14 版本起已棄用。請改用 buildCondition()

Defined in: yii\db\QueryBuilder::buildSimpleCondition()

建立類似 "column" operator value 的 SQL 表達式。

public string buildSimpleCondition ( $operator, $operands, &$params )
$operator 字串

要使用的運算子。可以使用任何符號,例如 ><= 等。

$operands 陣列

包含兩個欄位名稱。

$params 陣列

要填充的綁定參數

return 字串

生成的 SQL 表達式

throws yii\base\InvalidArgumentException

如果給定的運算元數量錯誤。

                public function buildSimpleCondition($operator, $operands, &$params)
{
    array_unshift($operands, $operator);
    return $this->buildCondition($operands, $params);
}

            
buildUnion() public method
public string buildUnion ( $unions, &$params )
$unions 陣列
$params 陣列

要填充的綁定參數

return 字串

yii\db\Query::$union 建構的 UNION 子句。

                public function buildUnion($unions, &$params)
{
    if (empty($unions)) {
        return '';
    }
    $result = '';
    foreach ($unions as $i => $union) {
        $query = $union['query'];
        if ($query instanceof Query) {
            list($unions[$i]['query'], $params) = $this->build($query, $params);
        }
        $result .= 'UNION ' . ($union['all'] ? 'ALL ' : '') . '( ' . $unions[$i]['query'] . ' ) ';
    }
    return trim($result);
}

            
buildWhere() public method
public string buildWhere ( $condition, &$params )
$condition string|array
$params 陣列

要填充的綁定參數

return 字串

yii\db\Query::$where 建構的 WHERE 子句。

                public function buildWhere($condition, &$params)
{
    $where = $this->buildCondition($condition, $params);
    return $where === '' ? '' : 'WHERE ' . $where;
}

            
buildWithQueries() public method (available since version 2.0.35)
public string buildWithQueries ( $withs, &$params )
$withs 陣列

每個 WITH 查詢的配置

$params 陣列

要填充的綁定參數

return 字串

已編譯的 WITH 查詢前綴,包含巢狀查詢

                public function buildWithQueries($withs, &$params)
{
    if (empty($withs)) {
        return '';
    }
    $recursive = false;
    $result = [];
    foreach ($withs as $i => $with) {
        if ($with['recursive']) {
            $recursive = true;
        }
        $query = $with['query'];
        if ($query instanceof Query) {
            list($with['query'], $params) = $this->build($query, $params);
        }
        $result[] = $with['alias'] . ' AS (' . $with['query'] . ')';
    }
    return 'WITH ' . ($recursive ? 'RECURSIVE ' : '') . implode(', ', $result);
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

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

如果屬性符合以下條件,則為可讀:

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

另請參閱 canSetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可讀

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

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

如果屬性符合以下條件,則為可寫:

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

另請參閱 canGetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可寫

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
checkIntegrity() public method

Defined in: yii\db\QueryBuilder::checkIntegrity()

建構 SQL 陳述式,用於啟用或停用完整性檢查。

public string checkIntegrity ( $check true, $schema '', $table '' )
$check boolean

是否開啟或關閉完整性檢查。

$schema 字串

資料表的結構描述。預設為空字串,表示目前或預設的結構描述。

$table 字串

資料表名稱。預設為空字串,表示不會變更任何資料表。

return 字串

用於檢查完整性的 SQL 語句

throws yii\base\NotSupportedException

如果底層 DBMS 不支援此功能

                public function checkIntegrity($check = true, $schema = '', $table = '')
{
    throw new NotSupportedException($this->db->getDriverName() . ' does not support enabling/disabling integrity check.');
}

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

Defined in: yii\base\BaseObject::className()

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

public static string className ( )
return 字串

此類別的完整限定名稱。

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

            
createConditionFromArray() public method (available since version 2.0.14)

Defined in: yii\db\QueryBuilder::createConditionFromArray()

根據 $conditionClasses 映射,將陣列格式定義的 $condition(如 yii\db\Query::where() 中所述)轉換為 yii\db\condition\ConditionInterface 的實例。

另請參閱 $conditionClasses

public yii\db\conditions\ConditionInterface createConditionFromArray ( $condition )
$condition string|array

                public function createConditionFromArray($condition)
{
    if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
        $operator = strtoupper(array_shift($condition));
        if (isset($this->conditionClasses[$operator])) {
            $className = $this->conditionClasses[$operator];
        } else {
            $className = 'yii\db\conditions\SimpleCondition';
        }
        /** @var ConditionInterface $className */
        return $className::fromArrayDefinition($operator, $condition);
    }
    // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
    return new HashCondition($condition);
}

            
createIndex() public method

Defined in: yii\db\QueryBuilder::createIndex()

建構 SQL 陳述式,用於建立新的索引。

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

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

$table 字串

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

$columns string|array

應包含在索引中的欄位。如果有複數個欄位,請用逗號分隔或使用陣列表示。每個欄位名稱都將由方法正確地加上引號,除非在名稱中找到括號。

$unique boolean

是否在新建立的索引上新增 UNIQUE 約束。

return 字串

用於建立新索引的 SQL 語句。

                public function createIndex($name, $table, $columns, $unique = false)
{
    return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ')
        . $this->db->quoteTableName($name) . ' ON '
        . $this->db->quoteTableName($table)
        . ' (' . $this->buildColumns($columns) . ')';
}

            
createTable() public method

Defined in: yii\db\QueryBuilder::createTable()

建構 SQL 陳述式,用於建立新的資料庫表格。

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

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

例如,

$sql = $queryBuilder->createTable('user', [
 'id' => 'pk',
 'name' => 'string',
 'age' => 'integer',
 'column_name double precision null default null', # definition only example
]);
public string createTable ( $table, $columns, $options null )
$table 字串

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

$columns 陣列

新資料表中的欄位(名稱 => 定義)。

$options string|null

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

return 字串

用於建立新 DB 資料表的 SQL 語句。

                public function createTable($table, $columns, $options = null)
{
    $cols = [];
    foreach ($columns as $name => $type) {
        if (is_string($name)) {
            $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $this->getColumnType($type);
        } else {
            $cols[] = "\t" . $type;
        }
    }
    $sql = 'CREATE TABLE ' . $this->db->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)";
    return $options === null ? $sql : $sql . ' ' . $options;
}

            
createView() public method (available since version 2.0.14)

Defined in: yii\db\QueryBuilder::createView()

建立 SQL 視圖。

public string createView ( $viewName, $subQuery )
$viewName 字串

要建立的檢視表名稱。

$subQuery string|yii\db\Query

定義檢視表的 select 語句。這可以是字串或 yii\db\Query 物件。

return 字串

CREATE VIEW SQL 語句。

                public function createView($viewName, $subQuery)
{
    if ($subQuery instanceof Query) {
        list($rawQuery, $params) = $this->build($subQuery);
        array_walk(
            $params,
            function (&$param) {
                $param = $this->db->quoteValue($param);
            }
        );
        $subQuery = strtr($rawQuery, $params);
    }
    return 'CREATE VIEW ' . $this->db->quoteTableName($viewName) . ' AS ' . $subQuery;
}

            
defaultConditionClasses() protected method (available since version 2.0.14)

Defined in: yii\db\QueryBuilder::defaultConditionClasses()

包含預設條件類別的陣列。如果您想變更查詢建構器的預設條件類別,請擴展此方法。請參閱 $conditionClasses 文件以取得詳細資訊。

另請參閱 $conditionClasses

protected array defaultConditionClasses ( )

                protected function defaultConditionClasses()
{
    return [
        'NOT' => 'yii\db\conditions\NotCondition',
        'AND' => 'yii\db\conditions\AndCondition',
        'OR' => 'yii\db\conditions\OrCondition',
        'BETWEEN' => 'yii\db\conditions\BetweenCondition',
        'NOT BETWEEN' => 'yii\db\conditions\BetweenCondition',
        'IN' => 'yii\db\conditions\InCondition',
        'NOT IN' => 'yii\db\conditions\InCondition',
        'LIKE' => 'yii\db\conditions\LikeCondition',
        'NOT LIKE' => 'yii\db\conditions\LikeCondition',
        'OR LIKE' => 'yii\db\conditions\LikeCondition',
        'OR NOT LIKE' => 'yii\db\conditions\LikeCondition',
        'EXISTS' => 'yii\db\conditions\ExistsCondition',
        'NOT EXISTS' => 'yii\db\conditions\ExistsCondition',
    ];
}

            
defaultExpressionBuilders() protected method (available since version 2.0.14)

包含預設表達式建構器的陣列。如果您想變更此查詢建構器的預設表達式建構器,請擴展並覆寫此方法。請參閱 $expressionBuilders 文件以取得詳細資訊。

protected array defaultExpressionBuilders ( )

                protected function defaultExpressionBuilders()
{
    return array_merge(parent::defaultExpressionBuilders(), [
        'yii\db\conditions\InCondition' => 'yii\db\oci\conditions\InConditionBuilder',
        'yii\db\conditions\LikeCondition' => 'yii\db\oci\conditions\LikeConditionBuilder',
    ]);
}

            
delete() public method

Defined in: yii\db\QueryBuilder::delete()

建立 DELETE SQL 陳述式。

例如,

$sql = $queryBuilder->delete('user', 'status = 0');

此方法將正確地跳脫資料表和欄位名稱。

public string delete ( $table, $condition, &$params )
$table 字串

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

$condition array|string

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

$params 陣列

綁定參數,此方法將修改這些參數,以便稍後將它們綁定到 DB 命令。

return 字串

DELETE SQL

                public function delete($table, $condition, &$params)
{
    $sql = 'DELETE FROM ' . $this->db->quoteTableName($table);
    $where = $this->buildWhere($condition, $params);
    return $where === '' ? $sql : $sql . ' ' . $where;
}

            
dropCheck() public method (available since version 2.0.13)

Defined in: yii\db\QueryBuilder::dropCheck()

建立 SQL 命令,用於刪除檢查約束。

public string dropCheck ( $name, $table )
$name 字串

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

$table 字串

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

return 字串

用於刪除檢查約束的 SQL 語句。

                public function dropCheck($name, $table)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);
}

            
dropColumn() public method

Defined in: yii\db\QueryBuilder::dropColumn()

建構 SQL 陳述式,用於刪除資料庫欄位。

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

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

$column 字串

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

return 字串

用於刪除 DB 欄位的 SQL 語句。

                public function dropColumn($table, $column)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' DROP COLUMN ' . $this->db->quoteColumnName($column);
}

            
dropCommentFromColumn() public method (available since version 2.0.8)

建構 SQL 命令,用於為欄位新增註解。

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

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

$column 字串

要註解的欄位的名稱。欄位名稱將由此方法正確地加上引號。

return 字串

用於為欄位新增註解的 SQL 陳述式

                public function dropCommentFromColumn($table, $column)
{
    return 'COMMENT ON COLUMN ' . $this->db->quoteTableName($table) . '.' . $this->db->quoteColumnName($column) . " IS ''";
}

            
dropCommentFromTable() public method (available since version 2.0.8)

建構 SQL 命令,用於為表格新增註解。

public string dropCommentFromTable ( $table )
$table 字串

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

return 字串

用於為欄位新增註解的 SQL 陳述式

                public function dropCommentFromTable($table)
{
    return 'COMMENT ON TABLE ' . $this->db->quoteTableName($table) . " IS ''";
}

            
dropDefaultValue() public method (available since version 2.0.13)

Defined in: yii\db\QueryBuilder::dropDefaultValue()

建立 SQL 命令,用於刪除預設值約束。

public string dropDefaultValue ( $name, $table )
$name 字串

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

$table 字串

要刪除其預設值約束的資料表。名稱將由方法正確地加上引號。

return 字串

用於刪除預設值約束的 SQL 語句。

throws yii\base\NotSupportedException

如果底層 DBMS 不支援此功能。

                public function dropDefaultValue($name, $table)
{
    throw new NotSupportedException($this->db->getDriverName() . ' does not support dropping default value constraints.');
}

            
dropForeignKey() public method

Defined in: yii\db\QueryBuilder::dropForeignKey()

建構 SQL 陳述式,用於刪除外來鍵約束。

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

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

$table 字串

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

return 字串

用於刪除外鍵約束的 SQL 語句。

                public function dropForeignKey($name, $table)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);
}

            
dropIndex() public method

建構 SQL 陳述式,用於刪除索引。

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

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

$table 字串

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

return 字串

用於刪除索引的 SQL 語句。

                public function dropIndex($name, $table)
{
    return 'DROP INDEX ' . $this->db->quoteTableName($name);
}

            
dropPrimaryKey() public method

Defined in: yii\db\QueryBuilder::dropPrimaryKey()

建構 SQL 陳述式,用於從現有表格移除主鍵約束。

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

要移除的主鍵約束名稱。

$table 字串

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

return 字串

用於從現有資料表中移除主鍵約束的 SQL 語句。

                public function dropPrimaryKey($name, $table)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);
}

            
dropTable() public method

Defined in: yii\db\QueryBuilder::dropTable()

建構 SQL 陳述式,用於刪除資料庫表格。

public string dropTable ( $table )
$table 字串

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

return 字串

用於刪除 DB 資料表的 SQL 語句。

                public function dropTable($table)
{
    return 'DROP TABLE ' . $this->db->quoteTableName($table);
}

            
dropUnique() public method (available since version 2.0.13)

Defined in: yii\db\QueryBuilder::dropUnique()

建立 SQL 命令,用於刪除唯一約束。

public string dropUnique ( $name, $table )
$name 字串

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

$table 字串

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

return 字串

用於刪除唯一約束的 SQL 語句。

                public function dropUnique($name, $table)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);
}

            
dropView() public method (available since version 2.0.14)

Defined in: yii\db\QueryBuilder::dropView()

刪除 SQL 視圖。

public string dropView ( $viewName )
$viewName 字串

要刪除的檢視表名稱。

return 字串

DROP VIEW SQL 語句。

                public function dropView($viewName)
{
    return 'DROP VIEW ' . $this->db->quoteTableName($viewName);
}

            
executeResetSequence() public method (available since version 2.0.16)

執行 SQL 陳述式,用於重設表格主鍵的序列值。

執行此操作的原因是一些資料庫(Oracle)需要多個查詢才能完成。序列會被重設,以便下一個插入的新列的主鍵將具有指定的值或現有最大值 +1。

public void executeResetSequence ( $table, $value null )
$table 字串

要重設其主鍵序列的資料表名稱

$value array|string|null

下一個插入的新列的主鍵值。如果未設定此值,則下一個新列的主鍵將具有現有最大值 +1。

throws yii\base\NotSupportedException

如果底層 DBMS 不支援此功能

                public function executeResetSequence($table, $value = null)
{
    $tableSchema = $this->db->getTableSchema($table);
    if ($tableSchema === null) {
        throw new InvalidArgumentException("Unknown table: $table");
    }
    if ($tableSchema->sequenceName === null) {
        throw new InvalidArgumentException("There is no sequence associated with table: $table");
    }
    if ($value !== null) {
        $value = (int) $value;
    } else {
        if (count($tableSchema->primaryKey) > 1) {
            throw new InvalidArgumentException("Can't reset sequence for composite primary key in table: $table");
        }
        // use master connection to get the biggest PK value
        $value = $this->db->useMaster(function (Connection $db) use ($tableSchema) {
            return $db->createCommand(
                'SELECT MAX("' . $tableSchema->primaryKey[0] . '") FROM "' . $tableSchema->name . '"'
            )->queryScalar();
        }) + 1;
    }
    //Oracle needs at least two queries to reset sequence (see adding transactions and/or use alter method to avoid grants' issue?)
    $this->db->createCommand('DROP SEQUENCE "' . $tableSchema->sequenceName . '"')->execute();
    $this->db->createCommand('CREATE SEQUENCE "' . $tableSchema->sequenceName . '" START WITH ' . $value
        . ' INCREMENT BY 1 NOMAXVALUE NOCACHE')->execute();
}

            
extractAlias() protected method (available since version 2.0.24)

Defined in: yii\db\QueryBuilder::extractAlias()

如果存在表格別名,則提取表格別名,否則傳回 false

protected boolean|array extractAlias ( $table )
$table

                protected function extractAlias($table)
{
    if (preg_match('/^(.*?)(?i:\s+as|)\s+([^ ]+)$/', $table, $matches)) {
        return $matches;
    }
    return false;
}

            
getColumnType() public method

Defined in: yii\db\QueryBuilder::getColumnType()

將抽象欄位類型轉換為實體欄位類型。

轉換是使用 $typeMap 中指定的類型映射完成的。支援以下抽象欄位類型(使用 MySQL 作為範例來說明相應的實體類型)

  • pk:自動遞增主鍵類型,將轉換為 "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"
  • bigpk:自動遞增主鍵類型,將轉換為 "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY"
  • upk:無符號自動遞增主鍵類型,將轉換為 "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY"
  • char:char 類型,將轉換為 "char(1)"
  • string:string 類型,將轉換為 "varchar(255)"
  • text:長字串類型,將轉換為 "text"
  • smallint:小整數類型,將轉換為 "smallint(6)"
  • integer:整數類型,將轉換為 "int(11)"
  • bigint:大整數類型,將轉換為 "bigint(20)"
  • boolean:布林類型,將轉換為 "tinyint(1)"
  • float:浮點數類型,將轉換為 "float"
  • decimal:十進制數類型,將轉換為 "decimal"
  • datetime:datetime 類型,將轉換為 "datetime"
  • timestamp:timestamp 類型,將轉換為 "timestamp"
  • time:time 類型,將轉換為 "time"
  • date:date 類型,將轉換為 "date"
  • money:money 類型,將轉換為 "decimal(19,4)"
  • binary:二進制資料類型,將轉換為 "blob"

如果抽象類型包含兩個或多個以空格分隔的部分(例如 "string NOT NULL"),則只會轉換第一部分,其餘部分將附加到轉換後的結果。例如,'string NOT NULL' 會轉換為 'varchar(255) NOT NULL'。

對於某些抽象類型,您也可以在類型後方直接加上圓括號來指定長度或精確度約束。例如,string(32) 在 MySQL 資料庫中會被轉換成 "varchar(32)"。如果底層的 DBMS 不支援這些類型的約束,則會被忽略。

如果在 $typeMap 中找不到類型,它將會不經任何變更地回傳。

public string getColumnType ( $type )
$type string|yii\db\ColumnSchemaBuilder

抽象欄位類型

return 字串

實體欄位類型。

                public function getColumnType($type)
{
    if ($type instanceof ColumnSchemaBuilder) {
        $type = $type->__toString();
    }
    if (isset($this->typeMap[$type])) {
        return $this->typeMap[$type];
    } elseif (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) {
        if (isset($this->typeMap[$matches[1]])) {
            return preg_replace('/\(.+\)/', '(' . $matches[2] . ')', $this->typeMap[$matches[1]]) . $matches[3];
        }
    } elseif (preg_match('/^(\w+)\s+/', $type, $matches)) {
        if (isset($this->typeMap[$matches[1]])) {
            return preg_replace('/^\w+/', $this->typeMap[$matches[1]], $type);
        }
    }
    return $type;
}

            
getExpressionBuilder() public 方法 (自版本 2.0.14 起可用)

定義於: yii\db\QueryBuilder::getExpressionBuilder()

取得適用於 $expression 的 yii\db\ExpressionBuilderInterface 物件。

使用 $expressionBuilders 陣列來尋找合適的 builder 類別。

另請參閱 $expressionBuilders

public yii\db\ExpressionBuilderInterface getExpressionBuilder ( yii\db\ExpressionInterface $expression )
$expression yii\db\ExpressionInterface
throws yii\base\InvalidArgumentException

當此 QueryBuilder 不支援 $expression 建構時。

                public function getExpressionBuilder(ExpressionInterface $expression)
{
    $className = get_class($expression);
    if (!isset($this->expressionBuilders[$className])) {
        foreach (array_reverse($this->expressionBuilders) as $expressionClass => $builderClass) {
            if (is_subclass_of($expression, $expressionClass)) {
                $this->expressionBuilders[$className] = $builderClass;
                break;
            }
        }
        if (!isset($this->expressionBuilders[$className])) {
            throw new InvalidArgumentException('Expression of class ' . $className . ' can not be built in ' . get_class($this));
        }
    }
    if ($this->expressionBuilders[$className] === __CLASS__) {
        return $this;
    }
    if (!is_object($this->expressionBuilders[$className])) {
        $this->expressionBuilders[$className] = new $this->expressionBuilders[$className]($this);
    }
    return $this->expressionBuilders[$className];
}

            
hasLimit() protected 方法

定義於: yii\db\QueryBuilder::hasLimit()

檢查給定的限制是否有效。

protected boolean hasLimit ( $limit )
$limit mixed

給定的 limit

return boolean

limit 是否有效

                protected function hasLimit($limit)
{
    return ($limit instanceof ExpressionInterface) || ctype_digit((string)$limit);
}

            
hasMethod() public 方法

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

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

預設實作是呼叫 php 函數 method_exists()。當您實作 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name 字串

方法名稱

return boolean

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasOffset() protected 方法

定義於: yii\db\QueryBuilder::hasOffset()

檢查給定的偏移量是否有效。

protected boolean hasOffset ( $offset )
$offset mixed

給定的 offset

return boolean

offset 是否有效

                protected function hasOffset($offset)
{
    return ($offset instanceof ExpressionInterface) || ctype_digit((string)$offset) && (string)$offset !== '0';
}

            
hasProperty() public 方法

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

傳回值,指示是否已定義屬性。

屬性在以下情況下被定義:

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

另請參閱

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

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

            
init() public 方法

定義於: yii\db\QueryBuilder::init()

初始化物件。

此方法在建構子結束時被調用,在物件使用給定的組態初始化之後。

public void init ( )

                public function init()
{
    parent::init();
    $this->expressionBuilders = array_merge($this->defaultExpressionBuilders(), $this->expressionBuilders);
    $this->conditionClasses = array_merge($this->defaultConditionClasses(), $this->conditionClasses);
}

            
insert() public 方法

定義於: yii\db\QueryBuilder::insert()

建立 INSERT SQL 陳述式。

例如, `php $sql = $queryBuilder->insert('user', [

'name' => 'Sam',
'age' => 30,

], $params); ` 此方法將正確地 escape 資料表和欄位名稱。

public string insert ( $table, $columns, &$params )
$table 字串

將在其中插入新列的表格。

$columns array|yii\db\Query

要插入到資料表中的欄位資料 (name => value) 或 Query 的實例,以執行 INSERT INTO ... SELECT SQL 陳述式。自版本 2.0.11 起,可以使用 Query

$params 陣列

將由此方法產生的綁定參數。它們應在稍後綁定到 DB 命令。

return 字串

INSERT SQL

                public function insert($table, $columns, &$params)
{
    list($names, $placeholders, $values, $params) = $this->prepareInsertValues($table, $columns, $params);
    return 'INSERT INTO ' . $this->db->quoteTableName($table)
        . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '')
        . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values);
}

            
prepareInsertSelectSubQuery() protected 方法 (自版本 2.0.11 起可用)

定義於: yii\db\QueryBuilder::prepareInsertSelectSubQuery()

為 INSERT INTO ... SELECT SQL 陳述式準備 select-subquery 和欄位名稱。

protected array prepareInsertSelectSubQuery ( $columns, $schema, $params = [] )
$columns yii\db\Query

代表 select query 的物件。

$schema yii\db\Schema

用於引用欄位名稱的 Schema 物件。

$params 陣列

要綁定到生成的 SQL 語句的參數。這些參數將與查詢建構過程中生成的其他參數一起包含在結果中。

return 陣列

欄位名稱、值和參數的陣列。

throws yii\base\InvalidArgumentException

如果 query 的 select 不僅包含具名參數。

                protected function prepareInsertSelectSubQuery($columns, $schema, $params = [])
{
    if (!is_array($columns->select) || empty($columns->select) || in_array('*', $columns->select)) {
        throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters');
    }
    list($values, $params) = $this->build($columns, $params);
    $names = [];
    $values = ' ' . $values;
    foreach ($columns->select as $title => $field) {
        if (is_string($title)) {
            $names[] = $schema->quoteColumnName($title);
        } elseif (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_\.]+)$/', $field, $matches)) {
            $names[] = $schema->quoteColumnName($matches[2]);
        } else {
            $names[] = $schema->quoteColumnName($field);
        }
    }
    return [$names, $values, $params];
}

            
prepareInsertValues() protected 方法 (自版本 2.0.14 起可用)

INSERT SQL 陳述式準備 VALUES 部分。

protected array prepareInsertValues ( $table, $columns, $params = [] )
$table 字串

將在其中插入新列的表格。

$columns array|yii\db\Query

要插入到資料表中的欄位資料 (name => value) 或 Query 的實例,以執行 INSERT INTO ... SELECT SQL 陳述式。

$params 陣列

將由此方法產生的綁定參數。它們應在稍後綁定到 DB 命令。

return 陣列

欄位名稱、佔位符、值和參數的陣列。

                protected function prepareInsertValues($table, $columns, $params = [])
{
    list($names, $placeholders, $values, $params) = parent::prepareInsertValues($table, $columns, $params);
    if (!$columns instanceof Query && empty($names)) {
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
        if ($tableSchema !== null) {
            $columns = !empty($tableSchema->primaryKey) ? $tableSchema->primaryKey : [reset($tableSchema->columns)->name];
            foreach ($columns as $name) {
                $names[] = $this->db->quoteColumnName($name);
                $placeholders[] = 'DEFAULT';
            }
        }
    }
    return [$names, $placeholders, $values, $params];
}

            
prepareUpdateSets() protected 方法 (自版本 2.0.14 起可用)

定義於: yii\db\QueryBuilder::prepareUpdateSets()

UPDATE SQL 陳述式準備 SET 部分。

protected array prepareUpdateSets ( $table, $columns, $params = [] )
$table 字串

要更新的資料表。

$columns 陣列

要更新的欄位資料 (name => value)。

$params 陣列

綁定參數,此方法將修改這些參數,以便稍後將它們綁定到 DB 命令。

return 陣列

UPDATE SQL 陳述式的 SET 部分陣列(第一個陣列元素)和參數(第二個陣列元素)。

                protected function prepareUpdateSets($table, $columns, $params = [])
{
    $tableSchema = $this->db->getTableSchema($table);
    $columnSchemas = $tableSchema !== null ? $tableSchema->columns : [];
    $sets = [];
    foreach ($columns as $name => $value) {
        $value = isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value;
        if ($value instanceof ExpressionInterface) {
            $placeholder = $this->buildExpression($value, $params);
        } else {
            $placeholder = $this->bindParam($value, $params);
        }
        $sets[] = $this->db->quoteColumnName($name) . '=' . $placeholder;
    }
    return [$sets, $params];
}

            
prepareUpsertColumns() protected 方法 (自版本 2.0.14 起可用)
protected array prepareUpsertColumns ( $table, $insertColumns, $updateColumns, &$constraints = [] )
$table 字串
$insertColumns array|yii\db\Query
$updateColumns array|boolean
$constraints yii\db\Constraint[]

此參數接收符合的 constraint 清單。constraints 將依其欄位名稱保持唯一。

                protected function prepareUpsertColumns($table, $insertColumns, $updateColumns, &$constraints = [])
{
    if ($insertColumns instanceof Query) {
        list($insertNames) = $this->prepareInsertSelectSubQuery($insertColumns, $this->db->getSchema());
    } else {
        $insertNames = array_map([$this->db, 'quoteColumnName'], array_keys($insertColumns));
    }
    $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints);
    $uniqueNames = array_map([$this->db, 'quoteColumnName'], $uniqueNames);
    if ($updateColumns !== true) {
        return [$uniqueNames, $insertNames, null];
    }
    return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)];
}

            
renameColumn() public 方法

定義於: yii\db\QueryBuilder::renameColumn()

建構 SQL 陳述式,用於重新命名欄位。

public string renameColumn ( $table, $oldName, $newName )
$table 字串

要重新命名的欄位所屬的資料表。名稱將由此方法正確地引用。

$oldName 字串

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

$newName 字串

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

return 字串

用於重新命名 DB 欄位的 SQL 陳述式。

                public function renameColumn($table, $oldName, $newName)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table)
        . ' RENAME COLUMN ' . $this->db->quoteColumnName($oldName)
        . ' TO ' . $this->db->quoteColumnName($newName);
}

            
renameTable() public 方法

建構 SQL 陳述式,用於重新命名資料庫表格。

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

要重新命名的資料表。名稱將由此方法正確地引用。

$newName 字串

新的資料表名稱。名稱將由此方法正確地引用。

return 字串

用於重新命名 DB 資料表的 SQL 陳述式。

                public function renameTable($table, $newName)
{
    return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' RENAME TO ' . $this->db->quoteTableName($newName);
}

            
resetSequence() public 方法

定義於: yii\db\QueryBuilder::resetSequence()

建立 SQL 陳述式,用於重設表格主鍵的序列值。

sequence 將被重置,以便下一個插入的新列的主鍵將具有指定的值或現有最大值 +1。

public string resetSequence ( $table, $value null )
$table 字串

將重置其主鍵 sequence 的資料表名稱

$value array|string|null

下一個插入的新列的主鍵值。如果未設定此值,則下一個新列的主鍵將具有現有最大值 +1。

return 字串

用於重置 sequence 的 SQL 陳述式

throws yii\base\NotSupportedException

如果底層 DBMS 不支援此功能

                public function resetSequence($table, $value = null)
{
    throw new NotSupportedException($this->db->getDriverName() . ' does not support resetting sequence.');
}

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

建立 SELECT EXISTS() SQL 陳述式。

public string selectExists ( $rawSql )
$rawSql 字串

要從中選取的原始形式的子查詢。

return 字串

SELECT EXISTS() SQL 陳述式。

                public function selectExists($rawSql)
{
    return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL';
}

            
setConditionClasses() public 方法 (自版本 2.0.14.2 起可用)

定義於: yii\db\QueryBuilder::setConditionClasses()

$conditionClasses 屬性的設定器。

另請參閱 $conditionClasses

public void setConditionClasses ( $classes )
$classes 字串[]

條件別名到條件類別的對應。例如

['LIKE' => yii\db\condition\LikeCondition::class]

                public function setConditionClasses($classes)
{
    $this->conditionClasses = array_merge($this->conditionClasses, $classes);
}

            
setExpressionBuilders() public 方法 (自版本 2.0.14 起可用)

定義於: yii\db\QueryBuilder::setExpressionBuilders()

$expressionBuilders 屬性的設定器。

另請參閱 $expressionBuilders

public void setExpressionBuilders ( $builders )
$builders 字串[]

應與 $expressionBuilders 屬性中預定義的 builders 合併的 builders 陣列。

                public function setExpressionBuilders($builders)
{
    $this->expressionBuilders = array_merge($this->expressionBuilders, $builders);
}

            
truncateTable() public 方法

定義於: yii\db\QueryBuilder::truncateTable()

建構 SQL 陳述式,用於截斷資料庫表格。

public string truncateTable ( $table )
$table 字串

要清空的資料表。名稱將由此方法正確地引用。

return 字串

用於清空 DB 資料表的 SQL 陳述式。

                public function truncateTable($table)
{
    return 'TRUNCATE TABLE ' . $this->db->quoteTableName($table);
}

            
update() public 方法

定義於: yii\db\QueryBuilder::update()

建立 UPDATE SQL 陳述式。

例如,

$params = [];
$sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params);

此方法將正確地跳脫資料表和欄位名稱。

public string update ( $table, $columns, $condition, &$params )
$table 字串

要更新的資料表。

$columns 陣列

要更新的欄位資料 (name => value)。

$condition array|string

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

$params 陣列

綁定參數,此方法將修改這些參數,以便稍後將它們綁定到 DB 命令。

return 字串

UPDATE SQL

                public function update($table, $columns, $condition, &$params)
{
    list($lines, $params) = $this->prepareUpdateSets($table, $columns, $params);
    $sql = 'UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . implode(', ', $lines);
    $where = $this->buildWhere($condition, $params);
    return $where === '' ? $sql : $sql . ' ' . $where;
}

            
upsert() public 方法 (自版本 2.0.14 起可用)

建立 SQL 陳述式,用於在資料庫表格中插入資料列(如果它們尚不存在,則比對唯一約束),或在它們已存在時更新它們。

例如,

$sql = $queryBuilder->upsert('pages', [
    'name' => 'Front page',
    'url' => 'https://example.com/', // url is unique
    'visits' => 0,
], [
    'visits' => new \yii\db\Expression('visits + 1'),
], $params);

此方法將正確地跳脫資料表和欄位名稱。

另請參閱 https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606

public string upsert ( $table, $insertColumns, $updateColumns, &$params )
$table 字串

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

$insertColumns array|yii\db\Query

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

$updateColumns array|boolean

如果欄位資料已存在,則要更新的欄位資料 (name => value)。如果傳遞 true,則欄位資料將被更新為與插入欄位資料匹配。如果傳遞 false,如果欄位資料已存在,則不會執行更新。

$params 陣列

將由此方法產生的綁定參數。它們應在稍後綁定到 DB 命令。

return 字串

產生的 SQL。

throws yii\base\NotSupportedException

如果底層 DBMS 不支援此功能。

                public function upsert($table, $insertColumns, $updateColumns, &$params)
{
    /** @var Constraint[] $constraints */
    list($uniqueNames, $insertNames, $updateNames) = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns, $constraints);
    if (empty($uniqueNames)) {
        return $this->insert($table, $insertColumns, $params);
    }
    if ($updateNames === []) {
        // there are no columns to update
        $updateColumns = false;
    }
    $onCondition = ['or'];
    $quotedTableName = $this->db->quoteTableName($table);
    foreach ($constraints as $constraint) {
        $constraintCondition = ['and'];
        foreach ($constraint->columnNames as $name) {
            $quotedName = $this->db->quoteColumnName($name);
            $constraintCondition[] = "$quotedTableName.$quotedName=\"EXCLUDED\".$quotedName";
        }
        $onCondition[] = $constraintCondition;
    }
    $on = $this->buildCondition($onCondition, $params);
    list(, $placeholders, $values, $params) = $this->prepareInsertValues($table, $insertColumns, $params);
    if (!empty($placeholders)) {
        $usingSelectValues = [];
        foreach ($insertNames as $index => $name) {
            $usingSelectValues[$name] = new Expression($placeholders[$index]);
        }
        $usingSubQuery = (new Query())
            ->select($usingSelectValues)
            ->from('DUAL');
        list($usingValues, $params) = $this->build($usingSubQuery, $params);
    }
    $mergeSql = 'MERGE INTO ' . $this->db->quoteTableName($table) . ' '
        . 'USING (' . (isset($usingValues) ? $usingValues : ltrim($values, ' ')) . ') "EXCLUDED" '
        . "ON ($on)";
    $insertValues = [];
    foreach ($insertNames as $name) {
        $quotedName = $this->db->quoteColumnName($name);
        if (strrpos($quotedName, '.') === false) {
            $quotedName = '"EXCLUDED".' . $quotedName;
        }
        $insertValues[] = $quotedName;
    }
    $insertSql = 'INSERT (' . implode(', ', $insertNames) . ')'
        . ' VALUES (' . implode(', ', $insertValues) . ')';
    if ($updateColumns === false) {
        return "$mergeSql WHEN NOT MATCHED THEN $insertSql";
    }
    if ($updateColumns === true) {
        $updateColumns = [];
        foreach ($updateNames as $name) {
            $quotedName = $this->db->quoteColumnName($name);
            if (strrpos($quotedName, '.') === false) {
                $quotedName = '"EXCLUDED".' . $quotedName;
            }
            $updateColumns[$name] = new Expression($quotedName);
        }
    }
    list($updates, $params) = $this->prepareUpdateSets($table, $updateColumns, $params);
    $updateSql = 'UPDATE SET ' . implode(', ', $updates);
    return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql";
}