Skip to content
General Revolution Evolution Add-ons International
Login | Register | MODX.com
MODX Open Source Content Management System, Framework, Platform and More.
Find a Partner | Hosts + SaaS | Jobs | Donate
  • RegisterSign Up with the MODX Community
  • LoginUse Your MODX.com Account
  • MODX Community Forums
  • General
  • Tips & Tricks
  •  
  • xPDO queries and MySQL reserved words#

  • 28042
    15,112
    How MODx Evo Works

    Log in to an Evo Manager username guest, password guestuser.

    sottwell Reply #1, 4 months, 1 week ago

    Reply
    • Link to this post#1
    I had occasion to use "key" as a field name. Because "key" is a MySQL reserved word, it caused a MySQL error when using this query

    $sort = $modx->getOption('sort',$scriptProperties,'key');
    $dir = $modx->getOption('dir',$scriptProperties,'ASC');
    
    $c = $modx->newQuery('Coupon');
    $c->sortby($sort,$dir);
    $coupons = $modx->getCollection('Coupon',$c);
    


    To solve the problem, since I really did want the field name to be "key", I changed the sort specification line to quote the field name:
    $sort = $modx->getOption('sort',$scriptProperties,'`key`');


    http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html



  • 22303
    9,770
    - MODX Chief Architect

    Jason Coward | MODX Chief Architect

    jasoncoward.com | @drumshaman

    opengeek Reply #2, 4 months, 1 week ago

    Reply
    • Link to this post#2
    Actually, xPDO provides a db agnostic way to solve this:

    $sort = $modx->getOption('sort',$scriptProperties,$modx->escape('key'));


    This will now support sqlsrv (and potentially sqlite, pgsql, etc.).

    The reason xPDOQuery->sort() does not automatically apply escaping is because it also accepts SQL functions and other syntax that would not necessarily be escaped itself.


  • 28042
    15,112
    How MODx Evo Works

    Log in to an Evo Manager username guest, password guestuser.

    sottwell Reply #3, 4 months, 1 week ago

    Reply
    • Link to this post#3
    I don't understand how escaping will solve the problem of using a reserved keyword as the field name, unless it automatically adds the backtick to it?


  • 22303
    9,770
    - MODX Chief Architect

    Jason Coward | MODX Chief Architect

    jasoncoward.com | @drumshaman

    opengeek Reply #4, 4 months, 1 week ago

    Reply
    • Link to this post#4
    That is what the escape character for MySQL is, the backtick. In sqlite, it is a single-quote. In sqlsrv, it's brackets.





Actions

Login to Post

Other Support Options

To file a bug or make a feature request visit our issue tracker, or you can also purchase commercial support.

Love MODX?

If you build sites for a living with MODX or just love using it, why not give back?

Information

Posted in this thread:
opengeek, sottwell

 
Back to Top

MODX Global HQ

1333 N Stemmons Fwy, Ste 110
Dallas, TX 75207
United States

+1 (469) 777-MODX (6639)

The MODX Company

  • Contact
  • Media Center
  • Careers at MODX
  • Wall of Fame
  • The MODX Blog

Sponsors

SoftLayer Firehost: Secure Cloud Hosting

Stay Connected

Read our previous email newsletters.

Twitter Facebook Google+ LinkedIn github Feeds

Privacy Policy | Terms of Service | Pixels by AKTA Web Studio© 2005-2012 MODX. All rights reserved. Trademark Policy