简体   繁体   English

CakePHP 3错误:SQLSTATE [42000]:语法错误或访问冲突:1064

[英]CakePHP 3 Error: SQLSTATE[42000]: Syntax error or access violation: 1064

I'm getting this error: 我收到此错误:

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

Here is the SQL query which is giving this error: 这是给出此错误的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

I enabled quoteIdentifiers config\\app, but it leads to this new error: 我启用了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

where the query becomes: 查询变为:

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

I think it's taking the 'Col from Colleges as the keyword 'COL', but I'm not sure. 我认为它将'Col from Colleges'作为关键字'COL',但是我不确定。 How to fix this? 如何解决这个问题?

This is the CakePHP code which is generating the MySQL query: 这是生成MySQL查询的CakePHP代码:

return $college_admins->find()
    ->select(['Colleges.*'])
    ->leftJoinWith('Colleges')
    ->where(['CollegeAdmins.user_id' => $userId]);

You cannot use Colleges.* in a CakePHP ORM query (CakePHP 3.x). 您不能在CakePHP ORM查询(CakePHP 3.x)中使用Colleges.* As you've discovered this creates incorrect SQL aliases like Colleges__* . 如您所知,这会创建不正确的SQL别名,例如Colleges__* Instead to select all columns of a table you need to pass a table object. 而是选择一个表的所有列,您需要传递一个表对象。

So you'd probably be wanting to do something like:- 所以您可能想要做类似的事情:-

->select($college_admins->Colleges)

Assuming Colleges is associated with your CollegeAdmins table. 假设Colleges与您的CollegeAdmins表相关联。

You cannot alias colleges.*, since this refers to all columns within colleges table and aliases refer to a single column (or table or subquery). 您不能为colleges。*加上别名,因为这是指colleges表中的所有列,而别名是指单个列(或表或子查询)。 You need to list all fields within the colleges table and provide an alias for each of them, such as 您需要列出colleges表中的所有字段,并为每个字段提供一个别名,例如

select colleges.ig as colleges_id, colleges.field1 as colleges_field1, ...

There is not syntax in sql to provide alias such way. sql中没有语法提供这种方式的别名。 What you may try to do is to access the metadata returned by mysql in php to retrieve the table name for each field. 您可能想做的是访问mysql在php中返回的元数据,以检索每个字段的表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM