介面 yii\db\QueryInterface
實作介面 | yii\db\ActiveQuery, yii\db\ActiveQueryInterface, yii\db\Query |
---|---|
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/db/QueryInterface.php |
QueryInterface 定義了資料庫查詢需要實作的最小方法集合。
此介面的預設實作由 yii\db\QueryTrait 提供。
它支援取得 one() 實例或 all()。 允許透過 limit() 和 offset() 進行分頁。 排序功能透過 orderBy() 支援,並且可以使用 where() 將項目限制為符合某些條件。
公共方法
方法 | 描述 | 定義於 |
---|---|---|
addOrderBy() | 將額外的 ORDER BY 欄位新增至查詢。 | yii\db\QueryInterface |
all() | 執行查詢並將所有結果作為陣列傳回。 | yii\db\QueryInterface |
andFilterWhere() | 在現有的 WHERE 條件中新增額外的條件,並忽略空的參數。 | yii\db\QueryInterface |
andWhere() | 在現有的 WHERE 條件中新增額外的條件。 | yii\db\QueryInterface |
count() | 傳回記錄的數量。 | yii\db\QueryInterface |
emulateExecution() | 設定是否模擬查詢執行,防止與資料儲存進行任何互動。 | yii\db\QueryInterface |
exists() | 傳回一個值,指示查詢結果是否包含任何資料列。 | yii\db\QueryInterface |
filterWhere() | 設定查詢的 WHERE 部分,並忽略空的參數。 | yii\db\QueryInterface |
indexBy() | 設定 indexBy() 屬性。 | yii\db\QueryInterface |
limit() | 設定查詢的 LIMIT 部分。 | yii\db\QueryInterface |
offset() | 設定查詢的 OFFSET 部分。 | yii\db\QueryInterface |
one() | 執行查詢並傳回單一結果列。 | yii\db\QueryInterface |
orFilterWhere() | 在現有的 WHERE 條件中新增額外的條件,並忽略空的參數。 | yii\db\QueryInterface |
orWhere() | 在現有的 WHERE 條件中新增額外的條件。 | yii\db\QueryInterface |
orderBy() | 設定查詢的 ORDER BY 部分。 | yii\db\QueryInterface |
where() | 設定查詢的 WHERE 部分。 | yii\db\QueryInterface |
方法詳情
將額外的 ORDER BY 欄位新增至查詢。
另請參閱 orderBy()。
public abstract $this addOrderBy ( $columns ) | ||
$columns | string|array |
要排序的欄位(和方向)。欄位可以使用字串(例如 "id ASC, name DESC")或陣列(例如 |
return | $this |
查詢物件本身 |
---|
public function addOrderBy($columns);
執行查詢並將所有結果作為陣列傳回。
public abstract array all ( $db = null ) | ||
$db | yii\db\Connection|null |
用於執行查詢的資料庫連線。如果未提供此參數,將會使用 |
return | array |
查詢結果。如果查詢沒有結果,將會傳回空陣列。 |
---|
public function all($db = null);
public abstract $this andFilterWhere ( array $condition ) | ||
$condition | array |
新的 WHERE 條件。請參考 where() 瞭解如何指定此參數。 |
return | $this |
查詢物件本身 |
---|
public function andFilterWhere(array $condition);
public abstract $this andWhere ( $condition ) | ||
$condition | array |
新的 WHERE 條件。請參考 where() 瞭解如何指定此參數。 |
return | $this |
查詢物件本身 |
---|
public function andWhere($condition);
傳回記錄的數量。
public abstract integer|string|null count ( $q = '*', $db = null ) | ||
$q | string |
COUNT 運算式。預設為 '*'。 |
$db | yii\db\Connection|null |
用於執行查詢的資料庫連線。如果未提供此參數,將會使用 |
return | integer|string|null |
記錄數量。 |
---|
public function count($q = '*', $db = null);
設定是否模擬查詢執行,防止與資料儲存進行任何互動。
在此模式啟用後,傳回查詢結果的方法,例如 one()、all()、exists() 等等,將傳回空值或 false 值。在您的程式邏輯指示查詢不應傳回任何結果時,您應該使用此方法,例如在您設定 false where 條件時,例如 0=1
。
public abstract $this emulateExecution ( $value = true ) | ||
$value | boolean |
是否防止查詢執行。 |
return | $this |
查詢物件本身。 |
---|
public function emulateExecution($value = true);
傳回一個值,指示查詢結果是否包含任何資料列。
public abstract boolean exists ( $db = null ) | ||
$db | yii\db\Connection|null |
用於執行查詢的資料庫連線。如果未提供此參數,將會使用 |
return | boolean |
查詢結果是否包含任何資料列。 |
---|
public function exists($db = null);
public abstract $this filterWhere ( array $condition ) | ||
$condition | array |
應放在 WHERE 部分的條件。請參考 where() 瞭解如何指定此參數。 |
return | $this |
查詢物件本身 |
---|
public function filterWhere(array $condition);
設定 indexBy() 屬性。
public abstract $this indexBy ( $column ) | ||
$column | string|callable |
查詢結果應依據索引的欄位名稱。這也可以是一個可呼叫的物件(例如匿名函式),根據給定的資料列資料傳回索引值。可呼叫物件的簽章應為
|
return | $this |
查詢物件本身 |
---|
public function indexBy($column);
設定查詢的 LIMIT 部分。
public abstract $this limit ( $limit ) | ||
$limit | integer|null |
限制。使用 null 或負值停用限制。 |
return | $this |
查詢物件本身 |
---|
public function limit($limit);
設定查詢的 OFFSET 部分。
public abstract $this offset ( $offset ) | ||
$offset | integer|null |
偏移量。使用 null 或負值停用偏移量。 |
return | $this |
查詢物件本身 |
---|
public function offset($offset);
執行查詢並傳回單一結果列。
public abstract array|boolean one ( $db = null ) | ||
$db | yii\db\Connection|null |
用於執行查詢的資料庫連線。如果未提供此參數,將會使用 |
return | array|boolean |
查詢結果的第一列(以陣列形式)。如果查詢沒有結果,則傳回 False。 |
---|
public function one($db = null);
public abstract $this orFilterWhere ( array $condition ) | ||
$condition | array |
新的 WHERE 條件。請參考 where() 瞭解如何指定此參數。 |
return | $this |
查詢物件本身 |
---|
public function orFilterWhere(array $condition);
public abstract $this orWhere ( $condition ) | ||
$condition | array |
新的 WHERE 條件。請參考 where() 瞭解如何指定此參數。 |
return | $this |
查詢物件本身 |
---|
public function orWhere($condition);
設定查詢的 ORDER BY 部分。
另請參閱 addOrderBy()。
public abstract $this orderBy ( $columns ) | ||
$columns | string|array |
要排序的欄位(和方向)。欄位可以使用字串(例如 "id ASC, name DESC")或陣列(例如 |
return | $this |
查詢物件本身 |
---|
public function orderBy($columns);
設定查詢的 WHERE 部分。
以陣列形式指定的 $condition
可以是以下兩種格式之一
- 雜湊格式:
['column1' => value1, 'column2' => value2, ...]
- 運算子格式:
[operator, operand1, operand2, ...]
雜湊格式的條件通常表示以下 SQL 運算式:column1=value1 AND column2=value2 AND ...
。如果值是陣列,則會產生 IN
運算式。如果值是 null
,則在產生的運算式中會使用 IS NULL
。以下是一些範例
['type' => 1, 'status' => 2]
產生(type = 1) AND (status = 2)
。['id' => [1, 2, 3], 'status' => 2]
產生(id IN (1, 2, 3)) AND (status = 2)
。['status' => null]
產生status IS NULL
。
運算子格式的條件會根據指定的運算子產生 SQL 運算式,運算子可以是下列其中之一
and:運算元應使用
AND
連接在一起。例如,['and', 'id=1', 'id=2']
將產生id=1 AND id=2
。如果運算元是陣列,則會使用此處描述的規則將其轉換為字串。例如,['and', 'type=1', ['or', 'id=1', 'id=2']]
將產生type=1 AND (id=1 OR id=2)
。此方法不會進行任何引號或跳脫。or:與
and
運算子類似,不同之處在於運算元使用OR
連接。例如,['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]
將產生(type IN (7, 8, 9) OR (id IN (1, 2, 3)))
。not:這只會接受一個運算元,並透過在查詢字串前面加上
NOT
來建立其否定。例如['not', ['attribute' => null]]
將產生條件NOT (attribute IS NULL)
。between:運算元 1 應為欄位名稱,運算元 2 和 3 應為欄位所在範圍的起始值和結束值。例如,
['between', 'id', 1, 10]
將產生id BETWEEN 1 AND 10
。not between:與
between
類似,不同之處在於產生的條件中,BETWEEN
被替換為NOT BETWEEN
。in:運算元 1 應為欄位或資料庫運算式,運算元 2 應為表示欄位或資料庫運算式應在值範圍內的陣列。例如,
['in', 'id', [1, 2, 3]]
將產生id IN (1, 2, 3)
。此方法將正確地為欄位名稱加上引號,並跳脫範圍中的值。若要建立複合
IN
條件,您可以使用欄位名稱和值的陣列,其中值依欄位名稱索引:['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']] ]
。您也可以指定一個子查詢,用於取得
IN
條件的值:['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
not in:與
in
運算子類似,不同之處在於產生的條件中,IN
被替換為NOT IN
。like:運算元 1 應為欄位或資料庫運算式,運算元 2 應為字串或陣列,表示欄位或資料庫運算式應像什麼。例如,
['like', 'name', 'tester']
將產生name LIKE '%tester%'
。當值範圍以陣列形式給定時,將會產生多個LIKE
述詞,並使用AND
連接。例如,['like', 'name', ['test', 'sample']]
將產生name LIKE '%test%' AND name LIKE '%sample%'
。此方法將正確地為欄位名稱加上引號,並跳脫值中的特殊字元。有時,您可能想要自行將百分比字元新增至比對值,您可以提供第三個運算元false
來執行此操作。例如,['like', 'name', '%tester', false]
將產生name LIKE '%tester'
。or like:與
like
運算子類似,不同之處在於當運算元 2 是陣列時,使用OR
來連接LIKE
述詞。not like:與
like
運算子類似,不同之處在於產生的條件中,LIKE
被替換為NOT LIKE
。or not like:與
not like
運算子類似,不同之處在於使用OR
來連接NOT LIKE
述詞。exists:運算元 1 是一個查詢物件,用於建立
EXISTS
條件。例如['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]
將產生以下 SQL 運算式:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)
。not exists:與
exists
運算子類似,不同之處在於產生的條件中,EXISTS
被替換為NOT EXISTS
。此外,您可以按如下方式指定任意運算子:
['>=', 'id', 10]
的條件將產生以下 SQL 運算式:id >= 10
。
請注意,此方法將覆寫任何現有的 WHERE 條件。您可能想要改用 andWhere() 或 orWhere()。
另請參閱
public abstract $this where ( $condition ) | ||
$condition | array |
應放在 WHERE 部分的條件。 |
return | $this |
查詢物件本身 |
---|
public function where($condition);
註冊 或 登入 以發表評論。