Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » Question on DataObject::get

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 376 Views
  • woodb2
    Community Member
    48 Posts

    Question on DataObject::get Link to this post

    The other day I posted a posted a question where I could get objects that had a particular ID

    jobsob = DataObject::get('Jobs', 'FamiliesID = 226');

    But I wanted to replace 226 with the ID of the class I'm in. I was given this as the solution:

    $jobsob = DataObject::get('Jobs', "FamiliesID = {$this->ID}"); And it worked perfectly. I'm just trying to modify it slightly and I get errors.

    What I want now.

    $jobsob = DataObject::get('Jobs', "FamilyTitle = {$this->Title}"); FamilyTitle is just a different field in Jobs and I want to get the all the Jobs where FamilyTitle is equal to the Title of the object I'm in.

    I'd appreciate any idea why this isn't working.

  • simon_w
    Forum Moderator
    474 Posts

    Re: Question on DataObject::get Link to this post

    This is because the database assumes any non-quoted string is a column name. What you want is "FamilyTitle = '{$this->Title}'". You must use single quotes as the SQL is run in ANSI mode, which means the database treats double quotes as wrappers for column/table names.

    If $this->Title could ever contain a single quote itself, you'll want something more like: "FamilyTitle = '" . Convert::raw2sql($this->Title) . "'"

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.