问题
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