CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 纪念品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与进来
    • 问题 (Github)
    • 烘焙坊
    • 特色资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 时事通讯
    • LinkedIn
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
      • 驱动程序
      • 异常
      • 表达式
      • 日志
      • 查询
      • 重试
      • 模式
      • 语句
      • 类型
    • 数据源
    • 错误
    • 事件
    • 表单
    • Http
    • I18n
    • 日志
    • 邮件
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用程序
    • 验证
    • 视图

类 FunctionExpression

此类表示 SQL 语句中的函数调用字符串。可以通过传递函数名称和参数列表来构造调用。出于安全原因,默认情况下所有传递的参数都会被引用,除非明确告知不引用。

命名空间: Cake\Database\Expression

属性摘要

  • $_conditions protected
    数组

    表示表达式树的“分支”的字符串或其他表达式对象列表。例如,数组的一个键可能看起来像 "sum > :value"

  • $_conjunction protected
    字符串

    用于连接此对象内部存储的每个内部表达式的字符串,例如 "AND"、"OR" 等。

  • $_name protected
    字符串

    在生成 SQL 字符串时要构造的函数的名称

  • $_returnType protected
    字符串

    此表达式执行时将返回的类型名称

  • $_typeMap protected
    Cake\Database\TypeMap|null

方法摘要

  • __clone() public

    克隆此对象及其表达式的子树。

  • __construct() public

    构造函数。接受要调用的函数的名称和要传递给函数的参数列表。可以选择传递一个类型列表,用于每个绑定参数。

  • _addConditions() protected

    用于分解嵌套条件数组并在此对象内部构建树结构以表示完整 SQL 表达式的辅助函数。字符串条件直接存储在条件中,而任何其他表示形式都包装在该类的适当实例或实例周围。

  • _calculateType() protected

    如果传递的字段存储在 typeMap 中,则返回该字段的类型名称

  • _castToExpression() protected

    如果类型类实现 ExpressionTypeInterface,则有条件地将传递的值转换为 ExpressionInterface 对象。否则,返回未修改的值。

  • _parseCondition() protected

    通过尝试提取字符串条件中的运算符(如果有)来解析字符串条件,最后返回适当的 QueryExpression 对象或条件的简单字符串表示形式。此函数负责生成占位符并将值替换为占位符,同时将值存储在其他位置以备将来绑定。

  • _requiresToExpressionCasting() protected

    返回一个数组,其中包含作为参数传递的类型名称列表中需要将值强制转换为表达式的类型。

  • add() public

    为函数调用添加一个或多个参数。

  • and() public

    返回一个新的 QueryExpression 对象,其中包含所有传递的条件并设置连接为 "AND"

  • between() public

    以 "field BETWEEN from AND to" 的形式将新的条件添加到表达式对象中。

  • case() public

    返回一个新的 case 表达式对象。

  • count() public

    函数的名称本身就是生成表达式,因此总是将存储在此对象中的表达式数量加 1。

  • eq() public

    以 "field = value" 的形式将新的条件添加到表达式对象中。

  • equalFields() public

    使用标识符包装构建相等条件或赋值。

  • exists() public

    以 "EXISTS (...)" 的形式将新的条件添加到表达式对象中。

  • getConjunction() public

    获取表达式树此级别条件当前配置的连接。

  • getDefaultTypes() public

    获取当前类型映射的默认类型。

  • getName() public

    获取此表达式中要调用的 SQL 函数的名称。

  • getReturnType() public

    获取此对象将生成的返回值的类型。

  • getTypeMap() public

    返回现有的类型映射。

  • gt() public

    以 "field > value" 的形式将新的条件添加到表达式对象中。

  • gte() public

    以 "field >= value" 的形式将新的条件添加到表达式对象中。

  • hasNestedExpression() public

    如果此表达式包含任何其他嵌套的 ExpressionInterface 对象,则返回 true

  • in() public

    以 "field IN (value1, value2)" 的形式将新的条件添加到表达式对象中。

  • isNotNull() public

    以 "field IS NOT NULL" 的形式将新的条件添加到表达式对象中。

  • isNull() public

    以 "field IS NULL" 的形式将新的条件添加到表达式对象中。

  • iterateParts() public

    为组成此表达式的每个部分执行回调。

  • like() public

    以 "field LIKE value" 的形式将新的条件添加到表达式对象中。

  • lt() public

    以 "field < value" 的形式将新的条件添加到表达式对象中。

  • lte() public

    以 "field <= value" 的形式将新的条件添加到表达式对象中。

  • not() public

    将新的条件集添加到树的此级别,并通过在前面添加 NOT 来否定最终结果,它看起来像 "NOT ( (condition1) AND (conditions2) )" 连接取决于当前为此对象配置的连接。

  • notEq() public

    以 "field != value" 的形式将新的条件添加到表达式对象中。

  • notExists() public

    以 "NOT EXISTS (...)" 的形式将新的条件添加到表达式对象中。

  • notIn() public

    以 "field NOT IN (value1, value2)" 的形式将新的条件添加到表达式对象中。

  • notInOrNull() public

    以 "(field NOT IN (value1, value2) OR field IS NULL" 的形式将新的条件添加到表达式对象中。

  • notLike() public

    以 "field NOT LIKE value" 的形式将新的条件添加到表达式对象中。

  • or() public

    返回一个新的 QueryExpression 对象,其中包含所有传递的条件并设置连接为 "OR"

  • setConjunction() public

    更改表达式树此级别条件的连接。

  • setDefaultTypes() public

    覆盖实现对象中字段的默认类型映射。

  • setName() public

    设置此表达式中要调用的 SQL 函数的名称。

  • setReturnType() public

    设置此对象将生成的返回值的类型。

  • setTypeMap() public

    如果 $typeMap 是一个数组,则创建一个新的 TypeMap,否则将其交换为给定的 TypeMap。

  • sql() public

    将节点转换为 SQL 字符串片段。

  • traverse() public

    递归地遍历表达式树的每个级别,为表达式的每个部分迭代,并将当前迭代的表达式的实例作为第一个参数传递给回调。

方法详细说明

__clone() ¶ public

__clone(): void

克隆此对象及其表达式的子树。

返回值
void

__construct() ¶ public

__construct(string $name, array $params = [], array<string, string>|array<string|null> $types = [], string $returnType = 'string')

构造函数。接受要调用的函数的名称和要传递给函数的参数列表。可以选择传递一个类型列表,用于每个绑定参数。

默认情况下,所有传递的参数都会被引用。如果要使用文字参数,则需要明确提示此函数。

示例

$f = new FunctionExpression('CONCAT', ['CakePHP', ' rules']);

上一行将生成 CONCAT('CakePHP', ' rules')

$f = new FunctionExpression('CONCAT', ['name' => 'literal', ' rules']);

将产生 CONCAT(name, ' rules')

参数
string $name

要构造的函数的名称

array $params optional

要传递给函数的参数列表 如果是关联数组,则键将在值为 'literal' 时用作参数

array<string, string>|array<string|null> $types optional

要与传递的参数关联的类型的关联数组

string $returnType optional

此表达式的返回类型

_addConditions() ¶ protected

_addConditions(array $conditions, array<int|string, string> $types): void

用于分解嵌套条件数组并在此对象内部构建树结构以表示完整 SQL 表达式的辅助函数。字符串条件直接存储在条件中,而任何其他表示形式都包装在该类的适当实例或实例周围。

参数
array $conditions

要存储在此对象中的条件列表

array<int|string, string> $types

与 $conditions 中引用的字段关联的类型列表

返回值
void

_calculateType() ¶ protected

_calculateType(Cake\Database\ExpressionInterface|string $field): string|null

如果传递的字段存储在 typeMap 中,则返回该字段的类型名称

参数
Cake\Database\ExpressionInterface|string $field

要获取类型的字段名称。

返回值
string|null

_castToExpression() ¶ protected

_castToExpression(mixed $value, string|null $type = null): mixed

如果类型类实现 ExpressionTypeInterface,则有条件地将传递的值转换为 ExpressionInterface 对象。否则,返回未修改的值。

参数
mixed $value

要转换为 ExpressionInterface 的值

string|null $type optional

类型名称

返回值
mixed

_parseCondition() ¶ protected

_parseCondition(string $condition, mixed $value): Cake\Database\ExpressionInterface|string

通过尝试提取字符串条件中的运算符(如果有)来解析字符串条件,最后返回适当的 QueryExpression 对象或条件的简单字符串表示形式。此函数负责生成占位符并将值替换为占位符,同时将值存储在其他位置以备将来绑定。

参数
string $condition

从中提取实际字段和运算符的值。

mixed $value

要绑定到字段占位符的值

返回值
Cake\Database\ExpressionInterface|string
Throws
InvalidArgumentException
如果运算符无效或在 NULL 使用时缺失。

_requiresToExpressionCasting() ¶ protected

_requiresToExpressionCasting(array $types): array

返回一个数组,其中包含作为参数传递的类型名称列表中需要将值强制转换为表达式的类型。

参数
array $types

类型名称列表

返回值
数组

add() ¶ public

add(Cake\Database\ExpressionInterface|array|string $conditions, array<string, string> $types = [], bool $prepend = false): $this

为函数调用添加一个或多个参数。

参数
Cake\Database\ExpressionInterface|array|string $conditions

要传递给函数的参数列表 如果是关联数组,则键将在值为 'literal' 时用作参数

array<string, string> $types optional

要与传递的参数关联的类型的关联数组

bool $prepend optional

是否追加或预先追加到参数列表

返回值
$this
See Also
\Cake\Database\Expression\FunctionExpression::__construct() 了解更多细节。

and() ¶ public

and(Cake\Database\ExpressionInterfaceClosure|array|string $conditions, array<string, string> $types = []): static

返回一个新的 QueryExpression 对象,其中包含所有传递的条件并设置连接为 "AND"

参数
Cake\Database\ExpressionInterfaceClosure|array|string $conditions

要使用 AND 连接

array<string, string> $types optional

指向传递值类型的字段的关联数组。用于将值正确绑定到语句。

返回值
static

between() ¶ public

between(Cake\Database\ExpressionInterface|string $field, mixed $from, mixed $to, string|null $type = null): $this

以 "field BETWEEN from AND to" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要比较的字段名称,用于确定范围内的值。

mixed $from

范围的初始值。

mixed $to

比较范围的结束值。

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

case() ¶ public

case(Cake\Database\ExpressionInterface|object|scalar|null $value = null, string|null $type = null): Cake\Database\Expression\CaseStatementExpression

返回一个新的 case 表达式对象。

当设置值时,生成的语法为 CASE case_value WHEN when_value ... END(简单 case),其中 when_value 与 case_value 进行比较。

当未设置值时,生成的语法为 CASE WHEN when_conditions ... END(搜索 case),其中条件包含比较。

请注意,null 是一个有效的 case 值,因此只有在您确实想要创建简单 case 表达式变体时才应传递它!

参数
Cake\Database\ExpressionInterface|object|scalar|null $value optional

case 值。

string|null $type optional

case 值类型。如果未提供类型,则将尝试从值中推断类型。

返回值
Cake\Database\Expression\CaseStatementExpression

count() ¶ public

count(): int

函数的名称本身就是生成表达式,因此总是将存储在此对象中的表达式数量加 1。

返回值
int

eq() ¶ public

eq(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field = value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。如果以 "[]" 结尾且值为数组,则将创建多个占位符,每个数组中的值一个。

返回值
$this

equalFields() ¶ public

equalFields(string $leftField, string $rightField): $this

使用标识符包装构建相等条件或赋值。

参数
string $leftField

左侧连接条件字段名称。

string $rightField

右侧连接条件字段名称。

返回值
$this

exists() ¶ public

exists(Cake\Database\ExpressionInterface $expression): $this

以 "EXISTS (...)" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface $expression

内部查询

返回值
$this

getConjunction() ¶ public

getConjunction(): string

获取表达式树此级别条件当前配置的连接。

返回值
字符串

getDefaultTypes() ¶ public

getDefaultTypes(): array<int|string, string>

获取当前类型映射的默认类型。

返回值
array<int|string, string>

getName() ¶ public

getName(): string

获取此表达式中要调用的 SQL 函数的名称。

返回值
字符串

getReturnType() ¶ public

getReturnType(): string

获取此对象将生成的返回值的类型。

返回值
字符串

getTypeMap() ¶ public

getTypeMap(): Cake\Database\TypeMap

返回现有的类型映射。

返回值
Cake\Database\TypeMap

gt() ¶ public

gt(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field > value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

gte() ¶ public

gte(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field >= value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

hasNestedExpression() ¶ public

hasNestedExpression(): bool

如果此表达式包含任何其他嵌套的 ExpressionInterface 对象,则返回 true

返回值
bool

in() ¶ public

in(Cake\Database\ExpressionInterface|string $field, Cake\Database\ExpressionInterface|array|string $values, string|null $type = null): $this

以 "field IN (value1, value2)" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

Cake\Database\ExpressionInterface|array|string $values

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

isNotNull() ¶ public

isNotNull(Cake\Database\ExpressionInterface|string $field): $this

以 "field IS NOT NULL" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要测试是否为非空的数据库字段

返回值
$this

isNull() ¶ public

isNull(Cake\Database\ExpressionInterface|string $field): $this

以 "field IS NULL" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要测试是否为空的数据库字段

返回值
$this

iterateParts() ¶ public

iterateParts(Closure $callback): $this

为组成此表达式的每个部分执行回调。

回调需要返回值,该返回值将替换当前访问的部分。如果回调返回 null,则该部分将完全从该表达式中丢弃。

回调函数将接收每个条件作为第一个参数,键作为第二个参数。可以将第二个参数声明为按引用传递,这将使您能够更改修改后的部分存储的键。

参数
Closure $callback

对每个部分运行的回调

返回值
$this

like() ¶ public

like(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field LIKE value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

lt() ¶ public

lt(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field < value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

lte() ¶ public

lte(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field <= value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

not() ¶ public

not(Cake\Database\ExpressionInterfaceClosure|array|string $conditions, array<string, string> $types = []): $this

将新的条件集添加到树的此级别,并通过在前面添加 NOT 来否定最终结果,它看起来像 "NOT ( (condition1) AND (conditions2) )" 连接取决于当前为此对象配置的连接。

参数
Cake\Database\ExpressionInterfaceClosure|array|string $conditions

要添加并取反

array<string, string> $types optional

指向传递值类型的字段的关联数组。用于将值正确绑定到语句。

返回值
$this

notEq() ¶ public

notEq(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field != value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。如果以 "[]" 结尾且值为数组,则将创建多个占位符,每个数组中的值一个。

返回值
$this

notExists() ¶ public

notExists(Cake\Database\ExpressionInterface $expression): $this

以 "NOT EXISTS (...)" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface $expression

内部查询

返回值
$this

notIn() ¶ public

notIn(Cake\Database\ExpressionInterface|string $field, Cake\Database\ExpressionInterface|array|string $values, string|null $type = null): $this

以 "field NOT IN (value1, value2)" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

Cake\Database\ExpressionInterface|array|string $values

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

notInOrNull() ¶ public

notInOrNull(Cake\Database\ExpressionInterface|string $field, Cake\Database\ExpressionInterface|array|string $values, string|null $type = null): $this

以 "(field NOT IN (value1, value2) OR field IS NULL" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

Cake\Database\ExpressionInterface|array|string $values

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

notLike() ¶ public

notLike(Cake\Database\ExpressionInterface|string $field, mixed $value, string|null $type = null): $this

以 "field NOT LIKE value" 的形式将新的条件添加到表达式对象中。

参数
Cake\Database\ExpressionInterface|string $field

要与值进行比较的数据库字段

mixed $value

要绑定到 $field 以进行比较的值

string|null $type optional

使用类型映射配置的 $value 的类型名称。

返回值
$this

or() ¶ public

or(Cake\Database\ExpressionInterfaceClosure|array|string $conditions, array<string, string> $types = []): static

返回一个新的 QueryExpression 对象,其中包含所有传递的条件并设置连接为 "OR"

参数
Cake\Database\ExpressionInterfaceClosure|array|string $conditions

要使用 OR 连接

array<string, string> $types optional

指向传递值类型的字段的关联数组。用于将值正确绑定到语句。

返回值
static

setConjunction() ¶ public

setConjunction(string $conjunction): $this

更改表达式树此级别条件的连接。

参数
string $conjunction

用于连接条件的值

返回值
$this

setDefaultTypes() ¶ public

setDefaultTypes(array<int|string, string> $types): $this

覆盖实现对象中字段的默认类型映射。

此方法在您需要设置跨多个查询函数/表达式共享的类型映射时很有用。

要添加默认值而不覆盖现有值,请使用 getTypeMap()->addDefaults()

参数
array<int|string, string> $types

要设置的类型数组。

返回值
$this
See Also
\Cake\Database\TypeMap::setDefaults()

setName() ¶ public

setName(string $name): $this

设置此表达式中要调用的 SQL 函数的名称。

参数
string $name

函数的名称

返回值
$this

setReturnType() ¶ public

setReturnType(string $type): $this

设置此对象将生成的返回值的类型。

参数
string $type

要返回的类型的名称

返回值
$this

setTypeMap() ¶ public

setTypeMap(Cake\Database\TypeMap|array $typeMap): $this

如果 $typeMap 是一个数组,则创建一个新的 TypeMap,否则将其交换为给定的 TypeMap。

参数
Cake\Database\TypeMap|array $typeMap

如果为数组,则创建 TypeMap,否则设置给定的 TypeMap

返回值
$this

sql() ¶ public

sql(Cake\Database\ValueBinder $binder): string

将节点转换为 SQL 字符串片段。

参数
Cake\Database\ValueBinder $binder
返回值
字符串

traverse() ¶ public

traverse(Closure $callback): $this

递归地遍历表达式树的每个级别,为表达式的每个部分迭代,并将当前迭代的表达式的实例作为第一个参数传递给回调。

参数
Closure $callback
返回值
$this

属性详情

$_conditions ¶ protected

表示表达式树的“分支”的字符串或其他表达式对象列表。例如,数组的一个键可能看起来像 "sum > :value"

类型
数组

$_conjunction ¶ protected

用于连接此对象内部存储的每个内部表达式的字符串,例如 "AND"、"OR" 等。

类型
字符串

$_name ¶ protected

在生成 SQL 字符串时要构造的函数的名称

类型
字符串

$_returnType ¶ protected

此表达式执行时将返回的类型名称

类型
字符串

$_typeMap ¶ protected

类型
Cake\Database\TypeMap|null
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与进来
  • 问题 (Github)
  • 烘焙坊
  • 特色资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 时事通讯
  • LinkedIn
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成