我收到此错误:
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作为关键词' COL'但我不确定。如何解决这个问题?
这是生成MySQL查询的CakePHP代码:
return $college_admins->find()
->select(['Colleges.*'])
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
答案 0 :(得分:1)
您不能在CakePHP ORM查询(CakePHP 3.x)中使用Colleges.*
。正如您所发现的,这会创建不正确的SQL别名,例如Colleges__*
。而是选择表的所有列,您需要传递一个表对象。
所以你可能想做类似的事情: -
->select($college_admins->Colleges)
假设Colleges
与您的CollegeAdmins
表相关联。
答案 1 :(得分:0)
你不能别名大学。*,因为这指的是大学表中的所有列,而别名指的是单个列(或表或子查询)。您需要列出Colleges表中的所有字段,并为每个字段提供别名,例如
select colleges.ig as colleges_id, colleges.field1 as colleges_field1, ...
sql中没有语法提供别名这样的方法。您可能尝试做的是访问php中mysql返回的元数据,以检索每个字段的表名。