how to add ` character to sql queries in cakephp 3

匆匆过客 提交于 2019-12-31 04:00:10

问题


I have a table with special fields name such as 'from' and 'order'

(this table used with another cms and I can't change table structor)

I want add a record using cakephp 3 ,but I got 'Database Error'.

it seems queries of cakephp 3 is not sanintized with ` character!

my code in controler:

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

generated SQL is:

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

SQL Query has syntax error...

What should I do?


回答1:


You need to add 'quoteIdentifiers' => true in your datasource configuration as stated in the doc

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




回答2:


Either you can configure your Datasources from 'quoteIdentifiers' => false to 'quoteIdentifiers' => true at app.php

OR

One of your columns is using a column name that is reserved by MySQL.

As you can see that FROM and ORDER is a reserve word by MYSQL, you can try changing these words from your database tables. Hoping it works.

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.



来源:https://stackoverflow.com/questions/32282940/how-to-add-character-to-sql-queries-in-cakephp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!