接口 yii\db\ActiveQueryInterface
| 扩展 | yii\db\QueryInterface |
|---|---|
| 实现于 | yii\db\ActiveQuery |
| 可用版本 | 2.0 |
| 源代码 | https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveQueryInterface.php |
ActiveQueryInterface 定义了活动记录查询类需要实现的通用接口。
这些方法用于返回活动记录的普通查询,也用于关系查询,其中查询表示两个活动记录类之间的关系,并且只会返回相关记录。
实现此接口的类还应该使用 yii\db\ActiveQueryTrait 和 yii\db\ActiveRelationTrait。
公共方法
方法详情
| public abstract $this addOrderBy ( $columns ) | ||
| $columns | string|array |
要按其排序的列(和方向)。列可以以字符串(例如“id ASC, name DESC”)或数组(例如 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function addOrderBy($columns);
定义于: yii\db\QueryInterface::all()
执行查询并将所有结果作为数组返回。
| public abstract array all ( $db = null ) | ||
| $db | yii\db\Connection|null |
用于执行查询的数据库连接。如果未提供此参数,则将使用 |
| 返回值 | array |
查询结果。如果查询结果为空,则将返回空数组。 |
|---|---|---|
public function all($db = null);
定义于: yii\db\QueryInterface::andFilterWhere()
忽略空参数,向现有条件添加额外的 WHERE 条件。
新的条件和现有条件将使用“AND”运算符连接。
另请参阅
| public abstract $this andFilterWhere ( array $condition ) | ||
| $condition | array |
新的 WHERE 条件。请参阅 where() 以了解如何指定此参数。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function andFilterWhere(array $condition);
| public abstract $this andWhere ( $condition ) | ||
| $condition | array |
新的 WHERE 条件。请参阅 where() 以了解如何指定此参数。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function andWhere($condition);
设置 asArray() 属性。
| public abstract $this asArray ( $value = true ) | ||
| $value | boolean |
是否以数组而不是活动记录的形式返回查询结果。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function asArray($value = true);
定义于: yii\db\QueryInterface::count()
返回记录数量。
| public abstract 整数|字符串|空 count ( $q = '*', $db = null ) | ||
| $q | 字符串 |
COUNT 表达式。默认为 '*'. |
| $db | yii\db\Connection|null |
用于执行查询的数据库连接。如果未提供此参数,则将使用 |
| 返回值 | 整数|字符串|空 |
记录数。 |
|---|---|---|
public function count($q = '*', $db = null);
定义于: yii\db\QueryInterface::emulateExecution()
设置是否模拟查询执行,阻止与数据存储的任何交互。
启用此模式后,返回查询结果的方法(如 one()、all()、exists() 等)将返回空值或 false。如果您的程序逻辑指示查询不应该返回任何结果,例如您设置了错误的 where 条件(如 0=1),则应使用此方法。
| public abstract $this emulateExecution ( $value = true ) | ||
| $value | boolean |
是否阻止查询执行。 |
| 返回值 | $this |
查询对象本身。 |
|---|---|---|
public function emulateExecution($value = true);
定义于: yii\db\QueryInterface::exists()
返回一个值,指示查询结果是否包含任何数据行。
| public abstract 布尔值 exists ( $db = null ) | ||
| $db | yii\db\Connection|null |
用于执行查询的数据库连接。如果未提供此参数,则将使用 |
| 返回值 | boolean |
查询结果是否包含任何数据行。 |
|---|---|---|
public function exists($db = null);
| public abstract $this filterWhere ( 数组 $condition ) | ||
| $condition | array |
应放在 WHERE 部分的条件。请参考 where() 以了解如何指定此参数。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function filterWhere(array $condition);
查找指定主记录的相关记录。
当以延迟方式访问 ActiveRecord 的关联时,将调用此方法。
| public abstract 混合 findFor ( $name, $model ) | ||
| $name | 字符串 |
关联名称 |
| $model | yii\db\ActiveRecordInterface |
主模型 |
| 返回值 | 混合 |
相关记录 |
|---|---|---|
public function findFor($name, $model);
设置 indexBy() 属性。
| public abstract $this indexBy ( $column ) | ||
| $column | 字符串|可调用 |
查询结果应按其索引的列名。这也可以是可调用对象(例如匿名函数),它根据给定的行或模型数据返回索引值。可调用对象的签名应为
|
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function indexBy($column);
定义于: yii\db\QueryInterface::limit()
设置查询的 LIMIT 部分。
| public abstract $this limit ( $limit ) | ||
| $limit | 整数|空 |
限制。使用 null 或负值禁用限制。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function limit($limit);
定义于: yii\db\QueryInterface::offset()
设置查询的 OFFSET 部分。
| public abstract $this offset ( $offset ) | ||
| $offset | 整数|空 |
偏移量。使用 null 或负值禁用偏移量。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function offset($offset);
执行查询并返回一行结果。
| public abstract yii\db\ActiveRecordInterface|数组|空 one ( $db = null ) | ||
| $db | yii\db\Connection|null |
用于创建 DB 命令的 DB 连接。如果为 |
| 返回值 | yii\db\ActiveRecordInterface|数组|空 |
查询结果的一行。根据 asArray() 的设置,查询结果可以是数组或 ActiveRecord 对象。如果查询结果为空,则返回 |
|---|---|---|
public function one($db = null);
| public abstract $this orFilterWhere ( 数组 $condition ) | ||
| $condition | array |
新的 WHERE 条件。请参阅 where() 以了解如何指定此参数。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function orFilterWhere(array $condition);
| public abstract $this orWhere ( $condition ) | ||
| $condition | array |
新的 WHERE 条件。请参阅 where() 以了解如何指定此参数。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function orWhere($condition);
| public abstract $this orderBy ( $columns ) | ||
| $columns | string|array |
要按其排序的列(和方向)。列可以以字符串(例如“id ASC, name DESC”)或数组(例如 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function orderBy($columns);
指定与联接表关联的关系,用于关系查询。
| public abstract $this via ( $relationName, callable $callable = null ) | ||
| $relationName | 字符串 |
关系名称。这指的是在关系的 primaryModel 中声明的关系。 |
| $callable | callable|null |
一个 PHP 回调函数,用于自定义与连接表关联的关系。其签名应为 |
| 返回值 | $this |
关系对象本身。 |
|---|---|---|
public function via($relationName, callable $callable = null);
定义于: yii\db\QueryInterface::where()
设置查询的 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 部分的条件。 |
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function where($condition);
指定应使用哪些关系执行此查询。
此方法的参数可以是一个或多个字符串,或者是一个包含关系名称和可选回调函数的数组,用于自定义关系。
关系名称可以指在 modelClass 中定义的关系,或者指代相关记录的关系的子关系。例如,orders.address 表示在与 orders 关系对应的模型类中定义的 address 关系。
以下是一些用法示例
// find customers together with their orders and country
Customer::find()->with('orders', 'country')->all();
// find customers together with their orders and the orders' shipping address
Customer::find()->with('orders.address')->all();
// find customers together with their country and orders of status 1
Customer::find()->with([
'orders' => function (\yii\db\ActiveQuery $query) {
$query->andWhere('status = 1');
},
'country',
])->all();
| public abstract $this with ( ) | ||
| 返回值 | $this |
查询对象本身 |
|---|---|---|
public function with();
注册 或 登录 以发表评论。