Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Significance of Backticks

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 366 Views
  • kpower
    Avatar
    Community Member
    7 Posts

    Significance of Backticks Link to this post

    I was going through some sample codes around the web, and found that backticks (`) are sometimes used to enclose the table names and field names we used in the queries in DataObject:get and DataObject:get_one methods. For example:

    $test = DataObject::get_one("Entity", "`Code`='".$data['Code']."'");

    What is the significance of enclosing the table names and filed names with backticks? I red some other sample codes in which no backticks are used. Is this something outdated now?

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Significance of Backticks Link to this post

    I'm not sure if that's the case now but in older versions of SS you needed the backticks in order for the framework to keep track of which table to actually fetch from depending on if you're on the draft or the published site. This is of course only relevant for versioned objects and primarily SiteTree and its descendants.

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Significance of Backticks Link to this post

    The only place I use it is when doing something like...

    SELECT * FROM Group

    because group is a reserved word... backticking is required all the time in the ORM because any relationshp, table or field may be a reserved word.

  • kpower
    Avatar
    Community Member
    7 Posts

    Re: Significance of Backticks Link to this post

    Got it, thank you guys!

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Significance of Backticks Link to this post

    You should really be using double quotes (") rather than back ticks (`) as back ticks are MySQL only and sapphire is designed to be database agnostic. This is especially important if you're releasing code as a module.

    Also, you should always quote database, table and column names. Once again this is mostly to be database agnostic, though there is also the possibility that in a future version of your current RDBMS, one of these names could become a reserved keyword, or you RDBMS may start treating any non-quoted string as a literal string.

    366 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.