我收到这个错误:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges O' at line 1
这是给出此错误的 SQL 查询:
SELECT Colleges.* AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges ON Colleges.id = (CollegeAdmins.college_id) WHERE CollegeAdmins.user_id = :c0 LIMIT 20 OFFSET 0
我启用了 quoteIdentifiers config\app,但它导致了这个新错误:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Col' at line 1
查询变为:
SELECT `Colleges`.* AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Colleges` ON `Colleges`.`id` = (`CollegeAdmins`.`college_id`) WHERE `CollegeAdmins`.`user_id` = :c0 LIMIT 20 OFFSET 0
我认为它将“Col from Colleges”作为关键字“COL”,但我不确定。如何解决这个问题?
这是生成 MySQL 查询的 CakePHP 代码:
return $college_admins->find()
->select(['Colleges.*'])
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
最佳答案
您不能在 CakePHP ORM 查询 (CakePHP 3.x) 中使用 Colleges.*
。正如您所发现的那样,这会创建不正确的 SQL 别名,例如 Colleges__*
。要选择表格的所有列,您需要传递一个表格对象。
所以你可能想要做这样的事情:-
->select($college_admins->Colleges)
假设 Colleges
与您的 CollegeAdmins
表关联。
关于php - CakePHP 3 错误 : SQLSTATE[42000]: Syntax error or access violation: 1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35318510/