Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

Question on DataObject::get


Reply


2 Posts   402 Views

Avatar
woodb2

Community Member, 48 Posts

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

Avatar
simon_w

Forum Moderator, 474 Posts

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) . "'"