How to safely use reserved SQL names?

前端 未结 3 1153
半阙折子戏
半阙折子戏 2020-11-29 13:50

I\'m using Cakephp 3 using sqlserver as datasource server. I am sure there\'s no problem with my database connection.. as home.ctp prompts that I am connected to my database

相关标签:
3条回答
  • 2020-11-29 14:26

    You can use this code before problematic query:

    $this->Tests->connection()->driver()->autoQuoting(true);
    

    and when you are finished you can turn auto quoting off:

    $this->Tests->connection()->driver()->autoQuoting(false);
    

    So bad performance would be only on problematic query.

    0 讨论(0)
  • 2020-11-29 14:35

    As already mentioned by Vishal Gajjar in the comments, you are using the reserved keyword desc for your column name, hence the error, it's not bakes fault, it's yours.

    In order to be able to use such reserved words, the column name needs to be quoted properly, however CakePHP 3 doesn't auto-quote by default anymore, as it's an expensive operation.

    If you insist on using reserved words, enable identifier quoting via the quoteIdentifiers option in your app.php config, or enable it manually using the autoQuoting() (enableAutoQuoting() as of CakePHP 3.4) method of the DB driver.

    See also

    • Cookbook > Database Access & ORM > Database Basics > Identifier Quoting
    • Cookbook > 3.x Migration Guide > New ORM Upgrade Guide > Identifier Quoting Disabled by Default
    • API > \Cake\Database\Driver::autoQuoting()
    • API > \Cake\Database\Driver::enableAutoQuoting()
    0 讨论(0)
  • 2020-11-29 14:43

    Use this :

    SELECT * FROM (SELECT Tests.id AS [Tests__id], Tests.[desc] AS [Tests__desc], 
    (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) AS [_cake_page_rownum_] FROM tests Tests) _cake_paging_
    WHERE _cake_paging_._cake_page_rownum_ <= :c0
    

    If you do use a keyword, use it in square braces [ ]

    0 讨论(0)
提交回复
热议问题