18591 Posts in 4875 Topics by 2285 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 127 Views |
-
Question on DataObject::get

8 October 2011 at 4:50am
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.
Brian -
Re: Question on DataObject::get

8 October 2011 at 6:48am
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) . "'"
| 127 Views | ||
|
Page:
1
|
Go to Top |

