'cakephp 3 - Impossible to generate condition with empty list
In cakephp 3 query gives error when added 'IN' condition in contain query.
$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();
Error
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
Tables are
- categories_students
category_id, student_id
students
category
$category_ids array
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
)
Please help
Solution 1:[1]
You can't leave IN
operator empty, you must put into it something like NULL
. Try to cover the case for an empty Array:
return $q->where(
['CategoriesStudents.category_id IN' =>
count($category_ids) > 0 ? $category_ids : [null])
]);
Reference: Empty IN clause parameter list in MySQL
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |