我的cakephp3应用程序运行良好,现在我希望通过关联的表字段来订购一些查询。
当按当前模型中的字段排序时,查询工作正常。但是如果我选择在关联表中按字段Models.field
进行排序,我现在可以获得14个SQL查询而不是一个查询。但真正的问题是ORDER BY
未包含在查询中。
我在CoursesEmployees模型中,该模型包含employee_id和course_id字段。
CoursesEmployeesController.php
public function certificate() {
$this->paginate = [
'sortWhitelist' => [
'Employees.surname', 'Courses.name', 'date_completed'
],
'conditions' => ['CoursesEmployees.completed' => true, 'Employees.user_id' => $this->Auth->user('id')],
'contain' => ['Employees', 'Courses'],
'order' => ['Employees.name'=>'asc']
];
$this->set('coursesEmployees', $this->paginate($this->CoursesEmployees));
}
如果我按'order' => ['date_created'=>'asc']
订购,我会收到一个查询,一切正常。
SELECT
CoursesEmployees.id AS `CoursesEmployees__id`,
CoursesEmployees.employee_id AS `CoursesEmployees__employee_id`,
CoursesEmployees.course_id AS `CoursesEmployees__course_id`,
CoursesEmployees.course_module_id AS `CoursesEmployees__course_module_id`,
CoursesEmployees.cid AS `CoursesEmployees__cid`,
CoursesEmployees.progress AS `CoursesEmployees__progress`,
CoursesEmployees.modified AS `CoursesEmployees__modified`,
CoursesEmployees.created AS `CoursesEmployees__created`,
CoursesEmployees.completed AS `CoursesEmployees__completed`,
CoursesEmployees.date_completed AS `CoursesEmployees__date_completed`,
Employees.id AS `Employees__id`,
Employees.user_id AS `Employees__user_id`,
Employees.hotel_id AS `Employees__hotel_id`,
Employees.name AS `Employees__name`,
Employees.email AS `Employees__email`,
Employees.surname AS `Employees__surname`,
Employees.employee_num AS `Employees__employee_num`,
Employees.modified AS `Employees__modified`,
Employees.created AS `Employees__created`,
Courses.id AS `Courses__id`,
Courses.name AS `Courses__name`,
Courses.course_lenght AS `Courses__course_lenght`
FROM
courses_employees CoursesEmployees
INNER JOIN
employees Employees
ON Employees.id = (
CoursesEmployees.employee_id
)
INNER JOIN
courses Courses
ON Courses.id = (
CoursesEmployees.course_id
)
WHERE
(
CoursesEmployees.completed = 1
AND Employees.user_id = '1'
)
ORDER BY
CoursesEmployees.date_completed asc LIMIT 20 OFFSET 0
如果我'order' => ['Employees.name'=>'asc']
(或字段名称的任何随机文本)我得到14个SQL查询,而主查询最后没有订单。
SHOW FULL COLUMNS FROM `courses_employees` 10 1
SHOW INDEXES FROM `courses_employees` 1 0
SELECT * FROM information_schema.key_column_usage AS kcu INNER JOIN information_schema.referential_constraints AS rc ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME) WHERE kcu.TABLE_SCHEMA = 'deep_mind' AND kcu.TABLE_NAME = 'courses_employees' and rc.TABLE_NAME = 'courses_employees' 0 0
SHOW TABLE STATUS WHERE Name = 'courses_employees' 1 0
SHOW FULL COLUMNS FROM `employees` 9 1
SHOW INDEXES FROM `employees` 1 0
SELECT * FROM information_schema.key_column_usage AS kcu INNER JOIN information_schema.referential_constraints AS rc ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME) WHERE kcu.TABLE_SCHEMA = 'deep_mind' AND kcu.TABLE_NAME = 'employees' and rc.TABLE_NAME = 'employees' 0 0
SHOW TABLE STATUS WHERE Name = 'employees' 1 0
SHOW FULL COLUMNS FROM `courses` 3 1
SHOW INDEXES FROM `courses` 1 0
SELECT * FROM information_schema.key_column_usage AS kcu INNER JOIN information_schema.referential_constraints AS rc ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME) WHERE kcu.TABLE_SCHEMA = 'deep_mind' AND kcu.TABLE_NAME = 'courses' and rc.TABLE_NAME = 'courses' 0 0
SHOW TABLE STATUS WHERE Name = 'courses' 1 0
SELECT
CoursesEmployees.id AS `CoursesEmployees__id`,
CoursesEmployees.employee_id AS `CoursesEmployees__employee_id`,
CoursesEmployees.course_id AS `CoursesEmployees__course_id`,
CoursesEmployees.course_module_id AS `CoursesEmployees__course_module_id`,
CoursesEmployees.cid AS `CoursesEmployees__cid`,
CoursesEmployees.progress AS `CoursesEmployees__progress`,
CoursesEmployees.modified AS `CoursesEmployees__modified`,
CoursesEmployees.created AS `CoursesEmployees__created`,
CoursesEmployees.completed AS `CoursesEmployees__completed`,
CoursesEmployees.date_completed AS `CoursesEmployees__date_completed`,
Employees.id AS `Employees__id`,
Employees.user_id AS `Employees__user_id`,
Employees.hotel_id AS `Employees__hotel_id`,
Employees.name AS `Employees__name`,
Employees.email AS `Employees__email`,
Employees.surname AS `Employees__surname`,
Employees.employee_num AS `Employees__employee_num`,
Employees.modified AS `Employees__modified`,
Employees.created AS `Employees__created`,
Courses.id AS `Courses__id`,
Courses.name AS `Courses__name`,
Courses.course_lenght AS `Courses__course_lenght`
FROM
courses_employees CoursesEmployees
INNER JOIN
employees Employees
ON Employees.id = (
CoursesEmployees.employee_id
)
INNER JOIN
courses Courses
ON Courses.id = (
CoursesEmployees.course_id
)
WHERE
(
CoursesEmployees.completed = 1
AND Employees.user_id = '1'
) LIMIT 20 OFFSET 0
答案 0 :(得分:5)
如果您立即重新运行脚本,您应该会看到这些查询消失。
这些查询用于获取数据以生成模式和其他元数据,默认情况下,这应该很少发生,因为在应用程序数据源配置中启用了cacheMetadata
,因此缓存了数据。
默认情况下,数据缓存2分钟,检查_cake_model_
的应用缓存配置。这是应用程序在调试模式下运行时将使用的值。如果不处于调试模式,默认设置为1年,请检查您的应用boostrap.php
。
另见
至于您缺少的订单字段问题,您错过了白名单中的Employees.name
字段,但即使将其他模型的字段列入白名单,它似乎也无效。
这似乎是分页器中的一个错误,即在PaginatorComponent::_prefix()
方法中,该字段被吞下,最终导致一个空的order
选项被传递给查询。
对于即将发布的3.0.7版本,我们已经报告了这个问题并且fixed。如果您不能等待,请抓住主分支或应用补丁。