## 查詢條件
DbModel支持使用鏈式調用來組裝where子句的查詢條件,并實現參數化查詢和值綁定。
~~~
DbModel::where($field, $operator = null, $value = null)
DbModel::andWhere($field, $operator = null, $value = null)
DbModel::orWhere($field, $operator = null, $value = null)
~~~
參數表:
| 參數名稱 | 必選 | 類型 | 說明 |
| --- | --- | --- | --- |
| field | 是 | string | 字段名 |
| operator | 否 | string | 比較關鍵字 |
| value | 否 | string,int,array | 比較值, operator為 in 或 not in 時, value必須為數組 |
* 當operator為“=”時,可省略此參數。
* operator可以是“=”、“>”、“>=”、“<”、“<=”、“in”,“not in”, “like”等比較符。
* 當比較符為 in 或 not in 時,value必須為數組,形如[1, 2, 3]
用例:
~~~
/**
* 模糊搜索獲取多行數據
*
* @return array
*/
public function testGetRowsByLike()
{
$fields = '*';
$query = $this->where('id', '>', 5)
->andWhere('id', '<', 20)
->andWhere('name', 'like', '%楊%');
return $query->getRows($fields);
}
/**
* 根據字段值列表獲取多行數據
*
* @return array
*/
public function testGetRowsByNames()
{
$fields = '*';
$rows = $this->where('id', '>', 5)
->andWhere('id', '<', 20)
->andWhere('name', 'in', ['楊文杰', '張三'])
->getRows($fields);
return $rows;
}
/**
* 滿足查詢條件的數據行數
*
* @return int
*/
public function testGetCount()
{
$count = $this->where('name', '楊文杰')
->andWhere('id', '<=', 15)
->getCount();
return $count;
}
~~~