首页 > 解决方案 > 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[]');

有任何想法吗?

标签: phpcakephp

解决方案


而不是这样复杂的例子,你可以试试这样:

// an array is not automatically converted
$result =  $this->table_xy->find('all')->where(['id IN' => $userIds, 
     'OR' => [
                   'status !=' => 1,
                   'name is NULL'
             ]
    ])->select(['id']);

CAKEPHP > 查询生成器 > 高级条件


推荐阅读