我需要在多列上创建一个 IN 条件,就像这样
I need to create a IN conditions on multiple columns, like this
...
WHERE
(order_date, order_number) IN (
('2016-03-11', 3455453),
('2016-03-18', 83545454),
('2016-06-17', 5354544)
)
从这样的数组开始:
$orders = [
['2016-03-11', 3455453],
['2016-03-18', 83545454],
['2016-06-17', 5354544]
];
使用 cake3 查询构建器.我试过
using cake3 query builder. I tried with
->where(['(order_date, order_number) IN' => $orders]);
但我收到一个错误:
无法将值转换为字符串
我知道手动创建操作数组的查询并不难,但我想知道是否有一种方法可以做到这一点.
I know it's not hard to manually create the query manipulating the array, but I'd like to know if there is a cake way to do it.
AFAICT 这是不可能的(还)使用数组语法或正则比较表达式,负责转换的代码仅处理单个字段和平面数组,请参阅
AFAICT this is not possible (yet) using the array syntax or regular comparison expressions, the code responsible for transforming only handles single fields and flat arrays, see
来源>CakeDatabaseExpressionComparison::_stringExpression()
然而,这很可能使用元组比较表达式,它支持开箱即用的处理元组集.在内部,它被关联用于处理复合键.
However, this is very well possible using a tuple comparison expression, which supports handling sets of tuples out of the box. Internally it is used by associations for handling composite keys.
$fields = ['order_date', 'order_number'];
$types = ['date', 'integer'];
$values = [
['2016-03-11', 3455453],
['2016-03-18', 83545454],
['2016-06-17', 5354544]
];
$query->where(
new CakeDatabaseExpressionTupleComparison($fields, $values, $types, 'IN')
);
来源> CakeDatabaseExpressionTupleComparison
这篇关于查询生成器:带有复合列的 IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽