類別 yii\db\sqlite\Command
Command 代表一個 SQLite 的 SQL 陳述式,將針對資料庫執行。
{@inheritdoc}
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$behaviors | yii\base\Behavior[] | 附加到此元件的行為列表。 | yii\base\Component |
$db | yii\db\Connection | 與此指令關聯的 DB 連線 | yii\db\Command |
$fetchMode | integer | 此指令的預設提取模式。 | yii\db\Command |
$params | array | 綁定到目前 PDO 陳述式的參數 (名稱 => 值)。 | yii\db\Command |
$pdoStatement | PDOStatement | 與此指令關聯的 PDOStatement 物件 | yii\db\Command |
$pendingParams | array | 待定參數,將綁定到目前的 PDO 陳述式。 | yii\db\Command |
$queryCacheDependency | yii\caching\Dependency | 要與此指令的快取查詢結果關聯的依賴性 | yii\db\Command |
$queryCacheDuration | integer | 查詢結果在快取中保持有效的預設秒數。 | yii\db\Command |
$rawSql | string | 原始 SQL,參數值已插入到 $sql 中對應的佔位符。 | yii\db\Command |
$sql | string | 要執行的 SQL 陳述式。 | yii\db\Command |
公開方法
受保護的方法
方法 | 描述 | 定義於 |
---|---|---|
bindPendingParams() | 綁定透過 bindValue() 和 bindValues() 註冊的待定參數。 | yii\db\Command |
getCacheKey() | 傳回查詢的快取金鑰。 | yii\db\Command |
internalExecute() | 執行準備好的陳述式。 | yii\db\Command |
logQuery() | 如果已啟用查詢記錄,則記錄目前的資料庫查詢,如果已啟用分析,則傳回分析權杖。 | yii\db\Command |
queryInternal() | 執行 SQL 陳述式的實際 DB 查詢。 | yii\db\sqlite\Command |
refreshTableSchema() | 重新整理資料表結構描述,該結構描述已由 requireTableSchemaRefresh() 標記。 | yii\db\Command |
requireTableSchemaRefresh() | 標記指定的資料表結構描述,以便在指令執行後重新整理。 | yii\db\Command |
requireTransaction() | 標記指令要在交易中執行。 | yii\db\Command |
reset() | 將指令屬性重設為其初始狀態。 | yii\db\Command |
setRetryHandler() | 設定可呼叫的項目 (例如匿名函式),當執行指令時擲回 yii\db\Exception 時會呼叫該項目。可呼叫項目的簽章應為 | yii\db\Command |
方法詳細資訊
定義於: yii\base\Component::__call()
呼叫未定義為類別方法的具名方法。
此方法將檢查是否有任何附加的行為具有具名方法,並在可用時執行它。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當調用未知方法時,將會隱含地呼叫它。
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | 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()");
}
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作會執行兩件事
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果此方法在子類別中被覆寫,建議
- 建構子的最後一個參數是組態陣列,如這裡的
$config
。 - 在建構子的結尾呼叫父類別實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值配對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: yii\base\Component::__get()
傳回元件屬性的值。
此方法將依以下順序檢查並採取相應的動作
- getter 定義的屬性:傳回 getter 結果
- 行為的屬性:傳回行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $component->property;
時,將會隱含地呼叫它。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值或行為的屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
throws | yii\base\InvalidCallException |
如果屬性是唯寫的。 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// 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);
}
定義於: yii\base\Component::__isset()
檢查屬性是否已設定,即已定義且非 null。
此方法將依以下順序檢查並採取相應的動作
- setter 定義的屬性:傳回屬性是否已設定
- 行為的屬性:傳回屬性是否已設定
- 對於不存在的屬性傳回
false
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property)
時,將會隱含地呼叫它。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | 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;
}
定義於: yii\base\Component::__set()
設定元件屬性的值。
此方法將依以下順序檢查並採取相應的動作
- setter 定義的屬性:設定屬性值
- 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
- 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
- 行為的屬性:設定行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $component->property = $value;
時,將會隱含地呼叫它。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性是唯讀的。 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// 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);
}
定義於: yii\base\Component::__unset()
將元件屬性設定為 null。
此方法將依以下順序檢查並採取相應的動作
- setter 定義的屬性:將屬性值設定為 null
- 行為的屬性:將屬性值設定為 null
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 unset($component->property)
時,將會隱含地呼叫它。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性是唯讀的。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
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);
}
定義於: yii\db\Command::addCheck()
建立 SQL 指令,用於將檢查約束新增到現有資料表。
public $this addCheck ( $name, $table, $expression ) | ||
$name | string |
$name |
$table | string |
將新增檢查約束的資料表。名稱將由方法正確地引用。 |
$expression | string |
|
return | $this |
命令物件本身。 |
---|
public function addCheck($name, $table, $expression)
{
$sql = $this->db->getQueryBuilder()->addCheck($name, $table, $expression);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::addColumn()
建立 SQL 指令,用於新增新的 DB 資料行。
public $this addColumn ( $table, $column, $type ) | ||
$table | string |
$table |
$column | string |
新資料行的名稱。名稱將由方法正確地引用。 |
$type | string |
資料行類型。yii\db\QueryBuilder::getColumnType() 將被呼叫以將給定的資料行類型轉換為物理類型。例如, |
return | $this |
命令物件本身 |
---|
public function addColumn($table, $column, $type)
{
$sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::addCommentOnColumn()
建立 SQL 指令,用於新增資料行註解。
public $this addCommentOnColumn ( $table, $column, $comment ) | ||
$table | string |
$table |
$column | string |
$column |
$comment | string |
要新增的註解文字。註解將由方法正確地引用。 |
return | $this |
命令物件本身 |
---|
public function addCommentOnColumn($table, $column, $comment)
{
$sql = $this->db->getQueryBuilder()->addCommentOnColumn($table, $column, $comment);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::addCommentOnTable()
建立 SQL 指令,用於新增資料表註解。
public $this addCommentOnTable ( $table, $comment ) | ||
$table | string |
$table |
$comment | string |
要新增的註解文字。註解將由方法正確地引用。 |
return | $this |
命令物件本身 |
---|
public function addCommentOnTable($table, $comment)
{
$sql = $this->db->getQueryBuilder()->addCommentOnTable($table, $comment);
return $this->setSql($sql);
}
定義於: yii\db\Command::addDefaultValue()
建立 SQL 指令,用於將預設值約束新增到現有資料表。
public $this addDefaultValue ( $name, $table, $column, $value ) | ||
$name | string |
$name |
$table | string |
$table |
$column | string |
$column |
$value | mixed |
$value |
return | $this |
命令物件本身。 |
---|
public function addDefaultValue($name, $table, $column, $value)
{
$sql = $this->db->getQueryBuilder()->addDefaultValue($name, $table, $column, $value);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public $this addForeignKey ( $name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null ) | ||
$name | string |
$name |
$table | string |
$table |
$columns | string|array |
$refTable |
外鍵參考的資料表。 | string |
$refColumns |
$delete | string|array |
$update |
addPrimaryKey() public method | string|null |
ON DELETE 選項。大多數 DBMS 支援這些選項:RESTRICT、CASCADE、NO ACTION、SET DEFAULT、SET NULL |
string|null | string|null |
ON UPDATE 選項。大多數 DBMS 支援這些選項:RESTRICT、CASCADE、NO ACTION、SET DEFAULT、SET NULL |
return | $this |
命令物件本身 |
---|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
$sql = $this->db->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
public $this addPrimaryKey ( $name, $table, $columns )
建立 SQL 指令,用於將主鍵約束新增到現有資料表。
此方法將正確地引用資料表和資料行名稱。
$name | ||
$name | string |
$table |
$table | string |
$columns |
$columns | string|array |
主鍵將包含的逗號分隔字串或資料行陣列。 |
return | $this |
命令物件本身。 |
---|
public function addPrimaryKey($name, $table, $columns)
{
$sql = $this->db->getQueryBuilder()->addPrimaryKey($name, $table, $columns);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::addUnique()
建立 SQL 指令,用於將唯一約束新增到現有資料表。
public $this addUnique ( $name, $table, $columns ) | ||
$name | string |
$name |
$table | string |
$table |
$columns | string|array |
$columns |
return | $this |
命令物件本身。 |
---|
public function addUnique($name, $table, $columns)
{
$sql = $this->db->getQueryBuilder()->addUnique($name, $table, $columns);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::alterColumn()
建立 SQL 指令,用於變更資料行的定義。
public $this alterColumn ( $table, $column, $type ) | ||
$table | string |
$table |
$column | string |
$column |
$type | string |
$type |
return | $this |
命令物件本身 |
---|
public function alterColumn($table, $column, $type)
{
$sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\base\Component::attachBehavior()
將行為附加到此元件。
此方法將根據給定的組態建立行為物件。之後,將透過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此元件。
另請參閱 detachBehavior()。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
$name |
$behavior | string|array|yii\base\Behavior |
行為組態。這可以是下列其中一項
|
return | yii\base\Behavior |
行為物件 |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
定義於: yii\base\Component::attachBehaviors()
將行為列表附加到元件。
每個行為都由其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或是用於建立行為的組態陣列。
另請參閱 attachBehavior()。
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
$behaviors |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義於: yii\db\Command::batchInsert()
建立批次 INSERT 指令。
例如,
$connection->createCommand()->batchInsert('user', ['name', 'age'], [
['Tom', 30],
['Jane', 20],
['Linda', 25],
])->execute();
此方法會正確地跳脫 (escape) 欄位名稱,並為要插入的值加上引號。
請注意,每一列中的值必須與對應的欄位名稱相符。
另請注意,建立的指令在 execute() 被呼叫之前不會執行。
public $this batchInsert ( $table, $columns, $rows ) | ||
$table | string |
新列將被插入的表格。 |
$columns | array |
欄位名稱 |
$rows | array|Generator |
要批量插入到表格中的列 |
return | $this |
命令物件本身 |
---|
public function batchInsert($table, $columns, $rows)
{
$table = $this->db->quoteSql($table);
$columns = array_map(function ($column) {
return $this->db->quoteSql($column);
}, $columns);
$params = [];
$sql = $this->db->getQueryBuilder()->batchInsert($table, $columns, $rows, $params);
$this->setRawSql($sql);
$this->bindValues($params);
return $this;
}
定義於: yii\base\Component::behaviors()
傳回此元件應表現為的行為列表。
子類別可以覆寫此方法,以指定它們要表現的行為 (behavior)。
此方法的傳回值應該是一個行為物件陣列或組態,以行為名稱作為索引。行為組態可以是指定行為類別的字串,或具有以下結構的陣列
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
請注意,行為類別必須繼承自 yii\base\Behavior。行為可以使用名稱或匿名方式附加。當使用名稱作為陣列鍵時,可以使用此名稱稍後使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。匿名行為無法檢索或分離。
在此方法中宣告的行為將自動附加到組件 (component)(依需求)。
public array behaviors ( ) | ||
return | array |
行為組態。 |
---|
public function behaviors()
{
return [];
}
定義於: yii\db\Command::bindParam()
將參數綁定到要執行的 SQL 陳述式。
另請參閱 https://php.dev.org.tw/manual/en/function.PDOStatement-bindParam.php。
public $this bindParam ( $name, &$value, $dataType = null, $length = null, $driverOptions = null ) | ||
$name | string|integer |
參數識別符。對於使用具名佔位符的預備語句,這將是 |
$value | mixed |
要綁定到 SQL 語句參數的 PHP 變數 (以參考方式傳遞) |
$dataType | integer|null |
參數的 SQL 資料類型。如果為 null,則類型由值的 PHP 類型決定。 |
$length | integer|null |
資料類型的長度 |
$driverOptions | mixed |
驅動程式特定的選項 |
return | $this |
目前正在執行的指令 |
---|
public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null)
{
$this->prepare();
if ($dataType === null) {
$dataType = $this->db->getSchema()->getPdoType($value);
}
if ($length === null) {
$this->pdoStatement->bindParam($name, $value, $dataType);
} elseif ($driverOptions === null) {
$this->pdoStatement->bindParam($name, $value, $dataType, $length);
} else {
$this->pdoStatement->bindParam($name, $value, $dataType, $length, $driverOptions);
}
$this->params[$name] = &$value;
return $this;
}
定義於: yii\db\Command::bindPendingParams()
綁定透過 bindValue() 和 bindValues() 註冊的待定參數。
請注意,此方法需要一個活動的 $pdoStatement。
protected void bindPendingParams ( ) |
protected function bindPendingParams()
{
foreach ($this->pendingParams as $name => $value) {
$this->pdoStatement->bindValue($name, $value[0], $value[1]);
}
$this->pendingParams = [];
}
定義於: yii\db\Command::bindValue()
將值綁定到參數。
另請參閱 https://php.dev.org.tw/manual/en/function.PDOStatement-bindValue.php。
public $this bindValue ( $name, $value, $dataType = null ) | ||
$name | string|integer |
參數識別符。對於使用具名佔位符的預備語句,這將是 |
$value | mixed |
要綁定到參數的值 |
$dataType | integer|null |
參數的 SQL 資料類型。如果為 null,則類型由值的 PHP 類型決定。 |
return | $this |
目前正在執行的指令 |
---|
public function bindValue($name, $value, $dataType = null)
{
if ($dataType === null) {
$dataType = $this->db->getSchema()->getPdoType($value);
}
$this->pendingParams[$name] = [$value, $dataType];
$this->params[$name] = $value;
return $this;
}
定義於: yii\db\Command::bindValues()
將值列表綁定到對應的參數。
這與 bindValue() 類似,不同之處在於它一次綁定多個值。請注意,每個值的 SQL 資料類型由其 PHP 類型決定。
public $this bindValues ( $values ) | ||
$values | array |
要綁定的值。這必須以關聯陣列的形式給出,陣列鍵是參數名稱,陣列值是相應的參數值,例如 |
return | $this |
目前正在執行的指令 |
---|
public function bindValues($values)
{
if (empty($values)) {
return $this;
}
$schema = $this->db->getSchema();
foreach ($values as $name => $value) {
if (is_array($value)) { // TODO: Drop in Yii 2.1
$this->pendingParams[$name] = $value;
$this->params[$name] = $value[0];
} elseif ($value instanceof PdoValue) {
$this->pendingParams[$name] = [$value->getValue(), $value->getType()];
$this->params[$name] = $value->getValue();
} else {
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
if ($value instanceof \BackedEnum) {
$value = $value->value;
} elseif ($value instanceof \UnitEnum) {
$value = $value->name;
}
}
$type = $schema->getPdoType($value);
$this->pendingParams[$name] = [$value, $type];
$this->params[$name] = $value;
}
}
return $this;
}
為此指令啟用查詢快取。
public $this cache ( $duration = null, $dependency = null ) | ||
$duration | integer|null |
此指令的查詢結果在快取中保持有效的秒數。如果未設定,則將改為使用 yii\db\Connection::$queryCacheDuration 的值。使用 0 表示快取資料永不過期。 |
$dependency | yii\caching\Dependency|null |
與快取查詢結果相關聯的快取依賴性。 |
return | $this |
命令物件本身 |
---|
public function cache($duration = null, $dependency = null)
{
$this->queryCacheDuration = $duration === null ? $this->db->queryCacheDuration : $duration;
$this->queryCacheDependency = $dependency;
return $this;
}
定義於: yii\base\Component::canGetProperty()
傳回值,指示是否可以讀取屬性。
如果符合以下條件,則可以讀取屬性:
- 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可讀屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | 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;
}
定義於: yii\base\Component::canSetProperty()
傳回值,指示是否可以設定屬性。
如果符合以下條件,則可以寫入屬性:
- 類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可寫入屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | 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;
}
public void cancel ( ) |
public function cancel()
{
$this->pdoStatement = null;
}
定義於: yii\db\Command::checkIntegrity()
建立 SQL 指令,用於啟用或停用完整性檢查。
public $this checkIntegrity ( $check = true, $schema = '', $table = '' ) | ||
$check | boolean |
是否開啟或關閉完整性檢查。 |
$schema | string |
表格的結構描述名稱。預設為空字串,表示當前或預設結構描述。 |
$table | string |
表格名稱。 |
return | $this |
命令物件本身 |
---|---|---|
throws | yii\base\NotSupportedException |
如果底層 DBMS 不支援此功能 |
public function checkIntegrity($check = true, $schema = '', $table = '')
{
$sql = $this->db->getQueryBuilder()->checkIntegrity($check, $schema, $table);
return $this->setSql($sql);
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
定義於: yii\db\Command::createIndex()
建立 SQL 指令,用於建立新的索引。
public $this createIndex ( $name, $table, $columns, $unique = false ) | ||
$name | string |
索引的名稱。名稱將由此方法正確地加上引號。 |
$table | string |
將在其上建立新索引的表格。表格名稱將由此方法正確地加上引號。 |
$columns | string|array |
應包含在索引中的欄位。如果有多個欄位,請用逗號分隔它們。欄位名稱將由此方法正確地加上引號。 |
$unique | boolean |
是否在新建立的索引上新增 UNIQUE 约束。 |
return | $this |
命令物件本身 |
---|
public function createIndex($name, $table, $columns, $unique = false)
{
$sql = $this->db->getQueryBuilder()->createIndex($name, $table, $columns, $unique);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::createTable()
建立 SQL 指令,用於建立新的 DB 資料表。
新表格中的欄位應指定為名稱-定義配對(例如 'name' => 'string'),其中 name 代表欄位名稱,將由此方法正確地加上引號,而 definition 代表欄位類型,其中必須包含抽象 DB 類型。
將呼叫 yii\db\QueryBuilder::getColumnType() 方法,將抽象欄位類型轉換為實體類型。例如,string
將轉換為 varchar(255)
,而 string not null
變為 varchar(255) not null
。
如果僅使用定義指定欄位(例如 'PRIMARY KEY (name, type)'),它將直接插入到產生的 SQL 中。
使用範例: `
php Yii::$app->db->createCommand()->createTable('post', [
'id' => 'pk',
'title' => 'string',
'text' => 'text',
'column_name double precision null default null',
]);
`
public $this createTable ( $table, $columns, $options = null ) | ||
$table | string |
要建立的表格名稱。名稱將由此方法正確地加上引號。 |
$columns | array |
新表格中的欄位(name => definition)。 |
$options | string|null |
將附加到產生的 SQL 的其他 SQL 片段。 |
return | $this |
命令物件本身 |
---|
public function createTable($table, $columns, $options = null)
{
$sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::createView()
建立 SQL 視圖。
public $this createView ( $viewName, $subquery ) | ||
$viewName | string |
要建立的視圖名稱。 |
$subquery | string|yii\db\Query |
定義視圖的 select 語句。這可以是字串或 yii\db\Query 物件。 |
return | $this |
命令物件本身。 |
---|
public function createView($viewName, $subquery)
{
$sql = $this->db->getQueryBuilder()->createView($viewName, $subquery);
return $this->setSql($sql)->requireTableSchemaRefresh($viewName);
}
建立 DELETE 指令。
例如,
$connection->createCommand()->delete('user', 'status = 0')->execute();
或使用參數綁定 (parameter binding) 進行條件設定
$status = 0;
$connection->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute();
此方法將正確地跳脫表格和欄位名稱。
請注意,建立的指令在 execute() 被呼叫之前不會執行。
public $this delete ( $table, $condition = '', $params = [] ) | ||
$table | string |
將從中刪除資料的表格。 |
$condition | string|array |
將放在 WHERE 部分的條件。請參閱 yii\db\Query::where() 以了解如何指定條件。 |
$params | array |
要綁定到指令的參數 |
return | $this |
命令物件本身 |
---|
public function delete($table, $condition = '', $params = [])
{
$sql = $this->db->getQueryBuilder()->delete($table, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
行為的名稱。 |
return | 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;
}
定義於: yii\base\Component::detachBehaviors()
從元件卸離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義於: yii\db\Command::dropCheck()
建立 SQL 指令,用於捨棄檢查約束。
public $this dropCheck ( $name, $table ) | ||
$name | string |
要刪除的 check 约束的名稱。名稱將由此方法正確地加上引號。 |
$table | string |
要刪除其 check 约束的表格。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身。 |
---|
public function dropCheck($name, $table)
{
$sql = $this->db->getQueryBuilder()->dropCheck($name, $table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropColumn()
建立 SQL 指令,用於捨棄 DB 資料行。
public $this dropColumn ( $table, $column ) | ||
$table | string |
要刪除其欄位的表格。名稱將由此方法正確地加上引號。 |
$column | string |
要刪除的欄位名稱。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function dropColumn($table, $column)
{
$sql = $this->db->getQueryBuilder()->dropColumn($table, $column);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropCommentFromColumn()
建立 SQL 指令,用於從資料行捨棄註解。
public $this dropCommentFromColumn ( $table, $column ) | ||
$table | string |
$table |
$column | string |
$column |
return | $this |
命令物件本身 |
---|
public function dropCommentFromColumn($table, $column)
{
$sql = $this->db->getQueryBuilder()->dropCommentFromColumn($table, $column);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropCommentFromTable()
建立 SQL 指令,用於從資料表捨棄註解。
public $this dropCommentFromTable ( $table ) | ||
$table | string |
$table |
return | $this |
命令物件本身 |
---|
public function dropCommentFromTable($table)
{
$sql = $this->db->getQueryBuilder()->dropCommentFromTable($table);
return $this->setSql($sql);
}
定義於: yii\db\Command::dropDefaultValue()
建立 SQL 指令,用於捨棄預設值約束。
public $this dropDefaultValue ( $name, $table ) | ||
$name | string |
要刪除的預設值约束的名稱。名稱將由此方法正確地加上引號。 |
$table | string |
要刪除其預設值约束的表格。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身。 |
---|
public function dropDefaultValue($name, $table)
{
$sql = $this->db->getQueryBuilder()->dropDefaultValue($name, $table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropForeignKey()
建立 SQL 指令,用於捨棄外鍵約束。
public $this dropForeignKey ( $name, $table ) | ||
$name | string |
要刪除的外鍵约束的名稱。名稱將由此方法正確地加上引號。 |
$table | string |
要刪除其外鍵的表格。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function dropForeignKey($name, $table)
{
$sql = $this->db->getQueryBuilder()->dropForeignKey($name, $table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropIndex()
建立 SQL 指令,用於捨棄索引。
public $this dropIndex ( $name, $table ) | ||
$name | string |
要刪除的索引名稱。名稱將由此方法正確地加上引號。 |
$table | string |
要刪除其索引的表格。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function dropIndex($name, $table)
{
$sql = $this->db->getQueryBuilder()->dropIndex($name, $table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropPrimaryKey()
建立 SQL 指令,用於移除現有資料表的主鍵約束。
public $this dropPrimaryKey ( $name, $table ) | ||
$name | string |
要移除的主鍵约束的名稱。 |
$table | string |
將從中移除主鍵约束的表格。 |
return | $this |
命令物件本身 |
---|
public function dropPrimaryKey($name, $table)
{
$sql = $this->db->getQueryBuilder()->dropPrimaryKey($name, $table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropTable()
建立 SQL 指令,用於捨棄 DB 資料表。
public $this dropTable ( $table ) | ||
$table | string |
要刪除的表格。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function dropTable($table)
{
$sql = $this->db->getQueryBuilder()->dropTable($table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropUnique()
建立 SQL 指令,用於捨棄唯一約束。
public $this dropUnique ( $name, $table ) | ||
$name | string |
要刪除的唯一約束名稱。 此名稱將由方法正確地加上引號。 |
$table | string |
要刪除唯一約束的表格。 此名稱將由方法正確地加上引號。 |
return | $this |
命令物件本身。 |
---|
public function dropUnique($name, $table)
{
$sql = $this->db->getQueryBuilder()->dropUnique($name, $table);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::dropView()
捨棄 SQL 視圖。
public $this dropView ( $viewName ) | ||
$viewName | string |
要刪除的視圖名稱。 |
return | $this |
命令物件本身。 |
---|
public function dropView($viewName)
{
$sql = $this->db->getQueryBuilder()->dropView($viewName);
return $this->setSql($sql)->requireTableSchemaRefresh($viewName);
}
定義於: 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);
}
}
}
執行 SQL 陳述式。
此方法應僅用於執行非查詢 SQL 陳述式,例如 INSERT
、DELETE
、UPDATE
SQL。 不會傳回任何結果集。
public integer execute ( ) | ||
return | integer |
執行所影響的列數。 |
---|---|---|
throws | yii\db\Exception |
執行失敗 |
public function execute()
{
$sql = $this->getSql();
$params = $this->params;
$statements = $this->splitStatements($sql, $params);
if ($statements === false) {
return parent::execute();
}
$result = null;
foreach ($statements as $statement) {
list($statementSql, $statementParams) = $statement;
$this->setSql($statementSql)->bindValues($statementParams);
$result = parent::execute();
}
$this->setSql($sql)->bindValues($params);
return $result;
}
定義於: yii\db\Command::executeResetSequence()
執行 db 指令,重設資料表主鍵的序列值。
執行原因是一些資料庫 (Oracle) 需要多個查詢才能執行此操作。 序列會被重置,以便下一個插入的新列的主鍵將具有指定的值或現有最大值 +1。
public void executeResetSequence ( $table, $value = null ) | ||
$table | string |
要重置主鍵序列的表格名稱 |
$value | mixed |
下一個插入的新列的主鍵值。 如果未設定,則下一個新列的主鍵將具有現有最大值 +1。 |
throws | yii\base\NotSupportedException |
如果底層 DBMS 不支援此功能 |
---|
public function executeResetSequence($table, $value = null)
{
return $this->db->getQueryBuilder()->executeResetSequence($table, $value);
}
定義於: yii\base\Component::getBehavior()
傳回具名的行為物件。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
行為名稱 |
return | yii\base\Behavior|null |
行為物件,如果行為不存在則為 null |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
定義於: yii\base\Component::getBehaviors()
傳回附加到此元件的所有行為。
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
附加到此組件的行為列表 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
定義於: yii\db\Command::getCacheKey()
傳回查詢的快取金鑰。
protected array getCacheKey ( $method, $fetchMode, $rawSql ) | ||
$method | string |
要呼叫的 PDOStatement 方法 |
$fetchMode | integer |
結果提取模式。 有效的提取模式請參考 PHP 手冊。 |
$rawSql | ||
return | array |
快取鍵 |
---|
protected function getCacheKey($method, $fetchMode, $rawSql)
{
$params = $this->params;
ksort($params);
return [
__CLASS__,
$method,
$fetchMode,
$this->db->dsn,
$this->db->username,
$this->getSql(),
json_encode($params),
];
}
定義於: yii\db\Command::getRawSql()
透過將參數值插入到 $sql 中對應的佔位符,傳回原始 SQL。
請注意,此方法的傳回值主要應用於記錄目的。 此方法很可能由於參數佔位符的不正確替換而傳回無效的 SQL。
public string getRawSql ( ) | ||
return | string |
原始 SQL,參數值已插入到 $sql 中對應的佔位符。 |
---|
public function getRawSql()
{
if (empty($this->params)) {
return $this->_sql;
}
$params = [];
foreach ($this->params as $name => $value) {
if (is_string($name) && strncmp(':', $name, 1)) {
$name = ':' . $name;
}
if (is_string($value) || $value instanceof Expression) {
$params[$name] = $this->db->quoteValue((string)$value);
} elseif (is_bool($value)) {
$params[$name] = ($value ? 'TRUE' : 'FALSE');
} elseif ($value === null) {
$params[$name] = 'NULL';
} elseif (!is_object($value) && !is_resource($value)) {
$params[$name] = $value;
}
}
if (!isset($params[1])) {
return preg_replace_callback('#(:\w+)#', function ($matches) use ($params) {
$m = $matches[1];
return isset($params[$m]) ? $params[$m] : $m;
}, $this->_sql);
}
$sql = '';
foreach (explode('?', $this->_sql) as $i => $part) {
$sql .= (isset($params[$i]) ? $params[$i] : '') . $part;
}
return $sql;
}
傳回此指令的 SQL 陳述式。
public string getSql ( ) | ||
return | string |
要執行的 SQL 陳述式 |
---|
public function getSql()
{
return $this->_sql;
}
定義於: yii\base\Component::hasEventHandlers()
傳回值,指示是否有任何處理常式附加到具名事件。
public boolean hasEventHandlers ( $name ) | ||
$name | string |
事件名稱 |
return | 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);
}
定義於: yii\base\Component::hasMethod()
傳回值,指示是否已定義方法。
如果滿足以下條件,則定義方法:
- 類別具有指定名稱的方法
- 附加的行為具有給定名稱的方法 (當
$checkBehaviors
為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkBehaviors | boolean |
是否將行為的方法視為此組件的方法 |
return | 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;
}
定義於: yii\base\Component::hasProperty()
傳回值,指示是否已為此元件定義屬性。
如果滿足以下條件,則定義屬性:
- 類別具有與指定名稱關聯的 getter 或 setter 方法 (在這種情況下,屬性名稱不區分大小寫);
- 類別具有帶有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的屬性 (當
$checkBehaviors
為 true 時)。
另請參閱
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為的屬性視為此組件的屬性 |
return | boolean |
是否已定義屬性 |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
public void init ( ) |
public function init()
{
}
建立 INSERT 指令。
例如,
$connection->createCommand()->insert('user', [
'name' => 'Sam',
'age' => 30,
])->execute();
此方法將正確地跳脫欄位名稱,並綁定要插入的值。
請注意,建立的指令在 execute() 被呼叫之前不會執行。
public $this insert ( $table, $columns ) | ||
$table | string |
新列將被插入的表格。 |
$columns | array|yii\db\Query |
要插入表格中的欄位資料 (name => value) 或 Query 的實例,以執行 INSERT INTO ... SELECT SQL 陳述式。 自 2.0.11 版本起可以使用傳遞 Query。 |
return | $this |
命令物件本身 |
---|
public function insert($table, $columns)
{
$params = [];
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
return $this->setSql($sql)->bindValues($params);
}
protected void internalExecute ( $rawSql ) | ||
$rawSql | string|null |
如果已建立 rawSql。 |
throws | yii\db\Exception |
如果執行失敗。 |
---|
protected function internalExecute($rawSql)
{
$attempt = 0;
while (true) {
try {
if (
++$attempt === 1
&& $this->_isolationLevel !== false
&& $this->db->getTransaction() === null
) {
$this->db->transaction(function () use ($rawSql) {
$this->internalExecute($rawSql);
}, $this->_isolationLevel);
} else {
$this->pdoStatement->execute();
}
break;
} catch (\Exception $e) {
$rawSql = $rawSql ?: $this->getRawSql();
$e = $this->db->getSchema()->convertException($e, $rawSql);
if ($this->_retryHandler === null || !call_user_func($this->_retryHandler, $e, $attempt)) {
throw $e;
}
}
}
}
定義於: yii\db\Command::logQuery()
如果已啟用查詢記錄,則記錄目前的資料庫查詢,如果已啟用分析,則傳回分析權杖。
protected array logQuery ( $category ) | ||
$category | string |
日誌類別。 |
return | array |
包含兩個元素的陣列,第一個是布林值,表示是否啟用效能分析。 第二個是 rawSql (如果已建立)。 |
---|
protected function logQuery($category)
{
if ($this->db->enableLogging) {
$rawSql = $this->getRawSql();
Yii::info($rawSql, $category);
}
if (!$this->db->enableProfiling) {
return [false, isset($rawSql) ? $rawSql : null];
}
return [true, isset($rawSql) ? $rawSql : $this->getRawSql()];
}
定義於: yii\db\Command::noCache()
停用此指令的查詢快取。
public $this noCache ( ) | ||
return | $this |
命令物件本身 |
---|
public function noCache()
{
$this->queryCacheDuration = -1;
return $this;
}
定義於: yii\base\Component::off()
從此元件卸離現有的事件處理常式。
此方法與 on() 相反。
注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理常式,而使用與此萬用字元匹配的純名稱註冊的處理常式將保留。
另請參閱 on()。
public boolean off ( $name, $handler = null ) | ||
$name | string |
事件名稱 |
$handler | callable|null |
要移除的事件處理常式。 如果為 null,則將移除附加到指定事件的所有處理常式。 |
return | 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;
}
將事件處理常式附加到事件。
事件處理常式必須是有效的 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 | string |
事件名稱 |
$handler | 可呼叫 |
事件處理常式 |
$data | mixed |
事件觸發時要傳遞給事件處理常式的資料。 當調用事件處理常式時,可以通過 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]);
}
}
定義於: yii\db\Command::prepare()
準備要執行的 SQL 陳述式。
對於要多次執行的複雜 SQL 陳述式,這可能會提高效能。 對於帶有綁定參數的 SQL 陳述式,此方法會自動調用。
public void prepare ( $forRead = null ) | ||
$forRead | boolean|null |
是否為讀取查詢調用此方法。 如果為 null,則表示應使用 SQL 陳述式來確定它是用於讀取還是寫入。 |
throws | yii\db\Exception |
如果存在任何資料庫錯誤 |
---|
public function prepare($forRead = null)
{
if ($this->pdoStatement) {
$this->bindPendingParams();
return;
}
$sql = $this->getSql();
if ($sql === '') {
return;
}
if ($this->db->getTransaction()) {
// master is in a transaction. use the same connection.
$forRead = false;
}
if ($forRead || $forRead === null && $this->db->getSchema()->isReadQuery($sql)) {
$pdo = $this->db->getSlavePdo(true);
} else {
$pdo = $this->db->getMasterPdo();
}
try {
$this->pdoStatement = $pdo->prepare($sql);
$this->bindPendingParams();
} catch (\Exception $e) {
$message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, $e->getCode(), $e);
} catch (\Throwable $e) {
$message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
throw new Exception($message, null, $e->getCode(), $e);
}
}
public yii\db\DataReader query ( ) | ||
return | yii\db\DataReader |
用於提取查詢結果的讀取器物件 |
---|---|---|
throws | yii\db\Exception |
執行失敗 |
public function query()
{
return $this->queryInternal('');
}
定義於: yii\db\Command::queryAll()
執行 SQL 陳述式並一次傳回所有資料列。
public array queryAll ( $fetchMode = null ) | ||
$fetchMode | integer|null |
結果提取模式。 有效的提取模式請參考 PHP 手冊。 如果此參數為 null,則將使用 $fetchMode 中設定的值。 |
return | array |
查詢結果的所有列。 每個陣列元素都是一個表示資料列的陣列。 如果查詢沒有結果,則傳回空陣列。 |
---|---|---|
throws | yii\db\Exception |
執行失敗 |
public function queryAll($fetchMode = null)
{
return $this->queryInternal('fetchAll', $fetchMode);
}
public array queryColumn ( ) | ||
return | array |
查詢結果的第一列。 如果查詢沒有結果,則傳回空陣列。 |
---|---|---|
throws | yii\db\Exception |
執行失敗 |
public function queryColumn()
{
return $this->queryInternal('fetchAll', \PDO::FETCH_COLUMN);
}
執行 SQL 陳述式的實際 DB 查詢。
protected mixed queryInternal ( $method, $fetchMode = null ) | ||
$method | string |
要呼叫的 PDOStatement 方法 |
$fetchMode | integer|null |
結果提取模式。 有效的提取模式請參考 PHP 手冊。 如果此參數為 null,則將使用 $fetchMode 中設定的值。 |
return | mixed |
方法執行結果 |
---|---|---|
throws | yii\db\Exception |
如果查詢導致任何問題 |
protected function queryInternal($method, $fetchMode = null)
{
$sql = $this->getSql();
$params = $this->params;
$statements = $this->splitStatements($sql, $params);
if ($statements === false) {
return parent::queryInternal($method, $fetchMode);
}
list($lastStatementSql, $lastStatementParams) = array_pop($statements);
foreach ($statements as $statement) {
list($statementSql, $statementParams) = $statement;
$this->setSql($statementSql)->bindValues($statementParams);
parent::execute();
}
$this->setSql($lastStatementSql)->bindValues($lastStatementParams);
$result = parent::queryInternal($method, $fetchMode);
$this->setSql($sql)->bindValues($params);
return $result;
}
public array|false queryOne ( $fetchMode = null ) | ||
$fetchMode | integer|null |
結果提取模式。 有效的提取模式請參考 PHP 手冊。 如果此參數為 null,則將使用 $fetchMode 中設定的值。 |
return | array|false |
查詢結果的第一列 (以陣列形式)。 如果查詢沒有結果,則傳回 False。 |
---|---|---|
throws | yii\db\Exception |
執行失敗 |
public function queryOne($fetchMode = null)
{
return $this->queryInternal('fetch', $fetchMode);
}
public string|integer|null|false queryScalar ( ) | ||
return | string|integer|null|false |
查詢結果的第一列中第一個值。 如果沒有值,則傳回 False。 |
---|---|---|
throws | yii\db\Exception |
執行失敗 |
public function queryScalar()
{
$result = $this->queryInternal('fetchColumn', 0);
if (is_resource($result) && get_resource_type($result) === 'stream') {
return stream_get_contents($result);
}
return $result;
}
定義於: yii\db\Command::refreshTableSchema()
重新整理資料表結構描述,該結構描述已由 requireTableSchemaRefresh() 標記。
protected void refreshTableSchema ( ) |
protected function refreshTableSchema()
{
if ($this->_refreshTableName !== null) {
$this->db->getSchema()->refreshTableSchema($this->_refreshTableName);
}
}
定義於: yii\db\Command::renameColumn()
建立 SQL 指令,用於重新命名資料行。
public $this renameColumn ( $table, $oldName, $newName ) | ||
$table | string |
要重新命名的欄位所在的表格。 此名稱將由方法正確地加上引號。 |
$oldName | string |
欄位的舊名稱。 此名稱將由方法正確地加上引號。 |
$newName | string |
欄位的新名稱。 此名稱將由方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function renameColumn($table, $oldName, $newName)
{
$sql = $this->db->getQueryBuilder()->renameColumn($table, $oldName, $newName);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::renameTable()
建立 SQL 指令,用於重新命名 DB 資料表。
public $this renameTable ( $table, $newName ) | ||
$table | string |
要重新命名的表格。 此名稱將由方法正確地加上引號。 |
$newName | string |
新的表格名稱。 此名稱將由方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function renameTable($table, $newName)
{
$sql = $this->db->getQueryBuilder()->renameTable($table, $newName);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}
定義於: yii\db\Command::requireTableSchemaRefresh()
標記指定的資料表結構描述,以便在指令執行後重新整理。
protected $this requireTableSchemaRefresh ( $name ) | ||
$name | string |
應重新整理結構描述的表格名稱。 |
return | $this |
此命令實例 |
---|
protected function requireTableSchemaRefresh($name)
{
$this->_refreshTableName = $name;
return $this;
}
定義於: yii\db\Command::requireTransaction()
標記指令要在交易中執行。
protected $this requireTransaction ( $isolationLevel = null ) | ||
$isolationLevel | string|null |
此交易要使用的隔離級別。詳情請參閱 yii\db\Transaction::begin()。 |
return | $this |
此命令實例。 |
---|
protected function requireTransaction($isolationLevel = null)
{
$this->_isolationLevel = $isolationLevel;
return $this;
}
將指令屬性重設為其初始狀態。
protected void reset ( ) |
protected function reset()
{
$this->_sql = null;
$this->pendingParams = [];
$this->params = [];
$this->_refreshTableName = null;
$this->_isolationLevel = false;
}
public $this resetSequence ( $table, $value = null ) | ||
$table | string |
將重設主鍵序列的表格名稱 |
$value | mixed |
下一個插入的新列的主鍵值。 如果未設定,則下一個新列的主鍵將具有現有最大值 +1。 |
return | $this |
命令物件本身 |
---|---|---|
throws | yii\base\NotSupportedException |
如果底層 DBMS 不支援此功能 |
public function resetSequence($table, $value = null)
{
$sql = $this->db->getQueryBuilder()->resetSequence($table, $value);
return $this->setSql($sql);
}
定義於: yii\db\Command::setRawSql()
指定要執行的 SQL 陳述式。SQL 陳述式將不會以任何方式修改。
先前的 SQL(如果有的話)將被捨棄,且 $params 也將被清除。詳情請參閱 reset()。
另請參閱
public $this setRawSql ( $sql ) | ||
$sql | string |
要設定的 SQL 陳述式。 |
return | $this |
此命令實例 |
---|
public function setRawSql($sql)
{
if ($sql !== $this->_sql) {
$this->cancel();
$this->reset();
$this->_sql = $sql;
}
return $this;
}
定義於: yii\db\Command::setRetryHandler()
設定可呼叫的項目 (例如匿名函式),當執行指令時擲回 yii\db\Exception 時會呼叫該項目。可呼叫項目的簽章應為
function (\yii\db\Exception $e, $attempt)
{
// return true or false (whether to retry the command or rethrow $e)
}
可呼叫物件將接收拋出的資料庫例外,以及從 1 開始的目前嘗試(執行命令)次數。
protected $this setRetryHandler ( callable $handler ) | ||
$handler | 可呼叫 |
用於處理資料庫例外的 PHP 回呼。 |
return | $this |
此命令實例。 |
---|
protected function setRetryHandler(callable $handler)
{
$this->_retryHandler = $handler;
return $this;
}
指定要執行的 SQL 陳述式。SQL 陳述式將使用 yii\db\Connection::quoteSql() 進行引用。
先前的 SQL(如果有的話)將被捨棄,且 $params 也將被清除。詳情請參閱 reset()。
另請參閱
public $this setSql ( $sql ) | ||
$sql | string |
要設定的 SQL 陳述式。 |
return | $this |
此命令實例 |
---|
public function setSql($sql)
{
if ($sql !== $this->_sql) {
$this->cancel();
$this->reset();
$this->_sql = $this->db->quoteSql($sql);
}
return $this;
}
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | string |
事件名稱 |
$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);
}
定義於: yii\db\Command::truncateTable()
建立 SQL 指令,用於截斷 DB 資料表。
public $this truncateTable ( $table ) | ||
$table | string |
要截斷的表格。名稱將由此方法正確地加上引號。 |
return | $this |
命令物件本身 |
---|
public function truncateTable($table)
{
$sql = $this->db->getQueryBuilder()->truncateTable($table);
return $this->setSql($sql);
}
建立 UPDATE 指令。
例如,
$connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
或使用參數綁定 (parameter binding) 進行條件設定
$minAge = 30;
$connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute();
此方法將正確地跳脫逸出字元欄位名稱,並將要更新的值繫結。
請注意,建立的指令在 execute() 被呼叫之前不會執行。
public $this update ( $table, $columns, $condition = '', $params = [] ) | ||
$table | string |
要更新的表格。 |
$columns | array |
要更新的欄位資料 (名稱 => 值)。 |
$condition | string|array |
將放在 WHERE 部分的條件。請參閱 yii\db\Query::where() 以了解如何指定條件。 |
$params | array |
要綁定到指令的參數 |
return | $this |
命令物件本身 |
---|
public function update($table, $columns, $condition = '', $params = [])
{
$sql = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
建立指令,用於將資料列插入資料庫資料表 (如果它們尚不存在,符合唯一約束),或在它們存在時更新它們。
例如,
$sql = $queryBuilder->upsert('pages', [
'name' => 'Front page',
'url' => 'https://example.com/', // url is unique
'visits' => 0,
], [
'visits' => new \yii\db\Expression('visits + 1'),
], $params);
此方法將正確地跳脫表格和欄位名稱。
public $this upsert ( $table, $insertColumns, $updateColumns = true, $params = [] ) | ||
$table | string |
新列將被插入/更新的表格。 |
$insertColumns | array|yii\db\Query |
要插入表格中的欄位資料 (名稱 => 值),或是要執行 |
$updateColumns | array|boolean |
如果欄位資料已存在,則要更新的欄位資料 (名稱 => 值)。如果傳遞 |
$params | array |
要繫結到命令的參數。 |
return | $this |
命令物件本身。 |
---|
public function upsert($table, $insertColumns, $updateColumns = true, $params = [])
{
$sql = $this->db->getQueryBuilder()->upsert($table, $insertColumns, $updateColumns, $params);
return $this->setSql($sql)->bindValues($params);
}
註冊 或 登入 以發表評論。