1

在 cakephp 3 中,当在包含查询中添加“IN”条件时,查询会出错。

$itemsquery = $Students->find('all');
$itemsquery->select(['Students.id']);

$itemsquery->matching('CategoriesStudents');
$itemsquery->contain([
'CategoriesStudents' => function ($q) use ($category_ids) {
return $q->where(['CategoriesStudents.category_id IN' => $category_ids]);
}
]);

// debug($itemsquery);
$items = $itemsquery->toArray(); 

错误

Impossible to generate condition with empty list of values for field (`CategoriesStudents`.`category_id`)

Error: An Internal Error Has Occurred.
An Internal Server Error Occurred

表是

  1. 类别_学生

类别 ID,学生 ID

  1. 学生

  2. 类别

$category_ids 数组

Array
(
    [1] => 57
    [2] => 60
    [3] => 58
    [4] => 13
    [5] => 78
    [6] => 56
    [7] => 59
    [8] => 79
    [9] => 95
    [10] => 94
    [11] => 96
    [12] => 93
    [13] => 82
    [14] => 23
    [15] => 82
    [16] => 14
    [17] => 16
    [18] => 101
    [19] => 14
)

请帮忙

4

1 回答 1

0

当您通过use ($category_ids)Cake 传递变量时,将执行查询。因此结果执行不会返回任何东西。

于 2021-10-18T17:34:12.217 回答