php - CakePHP 查询绑定
问题描述
我想用 cakePHP 3.6.10 创建一个 sql 语句:
SELECT id FROM table_xy WHERE (status != 1 OR name IS NULL) AND id IN(1,2,3);
现在,从食谱中的复杂示例中复制我得到了这个:
$userIds = [2,212,232];
$table = TableRegistry::getTableLocator()->get('TableXY');
$query = $table->find()
->select(['id'])
->where(function(QueryExpression $exp) {
$orConditions = $exp->or_(function($or) {
return $or->isNull('name')
->notEq('status', 1);
});
return $exp
->add($orConditions)
->in('id', ':id');
})
->bind(':id', $userIds, 'int[]');
$results = $query->all();
这会导致错误,说“未知类型 int[]”。但这与文档中描述的完全相同
$query->bind(':id', [1, 2, 3], 'int[]');
有任何想法吗?
解决方案
而不是这样复杂的例子,你可以试试这样:
// an array is not automatically converted
$result = $this->table_xy->find('all')->where(['id IN' => $userIds,
'OR' => [
'status !=' => 1,
'name is NULL'
]
])->select(['id']);
推荐阅读
- sql - 如何将多个参数值传递到 loadrunner 中的 SQLQuery
- button - 如何创建一个在没有自动注册/创建表单odoo13的情况下打开弹出窗口的按钮
- python - 如何同步python线程的启动时间?
- css - 禁用时更改文本文件的颜色
- python - `G` 类型的 python % 字符串格式如何表现?
- go - 什么是 shell 中 test -x 的 golang 等价物?
- html - 在 Xaringan Rmarkdown 演示文稿中使用 introjs
- node.js - 使用 express 时如何设置 nodejs 应用程序和 htaccess?
- c++ - 如何检测
OSX 上的可用性? - symfony - 在 Symfony Messenger 异步消息处理程序上使用选民/权限的最佳方式?