如何在 cakephp 3 中将 ` 字符添加到 sql 查询

how to add ` character to sql queries in cakephp 3

我有一个带有特殊字段名称的 table,例如 'from' 和 'order'

(这个 table 与另一个 cms 一起使用,我无法更改 table 结构)

我想使用 cakephp 3 添加记录,但我得到了 'Database Error'。

cakephp 3 的查询似乎没有用 ` 字符清理!

我在控制器中的代码:

$tour = $this->Tours->patchEntity($tour, $this->request->data);
$this->Tours->save($tour);

生成的SQL是:

INSERT INTO tour_tours 
       (title, from , to, description, duration) 
VALUES 
       ('tour title', 'from', 'to', 'description of tour', 7)

SQL 查询有语法错误...

我该怎么办?

您需要添加 'quoteIdentifiers' => true 在文档中所述的数据源配置中

http://book.cakephp.org/3.0/en/orm/database-basics.html#configuration

您可以将 Datasources 从 'quoteIdentifiers' => false 设置为 'quoteIdentifiers' => true app.php

您的其中一个列正在使用 MySQL 保留的列名。

你可以看到FROMORDER是MYSQL的保留字,你可以试试把这些字改一下从你的数据库表。希望有用。

For example: "tour_tours" table has a "from" field which makes you the Database Error. so please change these fields from your table and re-try.