CakePHP查询绑定

时间:2018-10-01 15:28:11

标签: 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);

现在,从cookbook中复杂的示例中复制过来,我得到了:

$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();

这将导致错误,提示“ unknown type int []”。但这与documentation

中描述的完全相同
$query->bind(':id', [1, 2, 3], 'int[]');

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以像这样尝试复杂的例子,

// 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 > Query Builder > Advanced Conditions