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.

Data Model Questions

Force ignore case


Go to End
Reply

9 Posts   1971 Views

Avatar
jdi

16 February 2010 at 10:43am Community Member, 34 Posts

Hey Guys,

Long time no see. Here's a question which I would love some assistance with :D

Can you force silverstripe to ignore character case in a string?

e.g print($item->Title);

$item = 'iron man', but the entry in the database is, of course; 'Iron Man'. Can you get silverstripe to use the $item value anyway without calling a "[Notice] Trying to get property of non-object" error? As mysql can handle the $item value just fine...

Avatar
zenmonkey

16 February 2010 at 11:16am Community Member, 528 Posts

Not sure if I totally follow what you're trying to do. But when I need to use User Inputed Values as variables in other parts of my program I usually scrub them by removing the spaces and/or convert to lowercase with something like:

$myVariable= strtolower(str_replace(" ","",$item));

Avatar
MateuszU

16 February 2010 at 11:42am Community Member, 89 Posts

But if $item is 'iron man' then why are you calling $item->Title?

Avatar
banal

16 February 2010 at 11:51am Community Member, 901 Posts

Yeah, what mateuzs said. I don't think this is a character-case issue.
The error suggests, that you're trying to access a property of a non-object. Since $item is a string ('iron man') it doesn't have a Title property.

Avatar
jdi

18 February 2010 at 3:11pm Community Member, 34 Posts

Cool, yea the issue is more complicated than above but the full extent of it would be hard to read through in a single post. I'll elaborate:

The above still applies but I need to include that it is in fact the Title of the item in the database that has the entry "Iron Man".

So to get the error you would apply:

$item= 'iron man';
      
$databaseItem= DataObject::get_one('Item' , '`Title` = \''.$item.'\'');         
         
print($databaseItem->Title);

When I do this the script will return "Iron Man". But silverstripe will return "[Notice] Trying to get property of non-object" and kill the site.

Avatar
MateuszU

18 February 2010 at 3:26pm (Last edited: 18 February 2010 4:45pm), Community Member, 89 Posts

Do a var dump on $databaseItem and check if it is an object. Is your MySQL running in ANSI mode?

Avatar
banal

18 February 2010 at 8:49pm Community Member, 901 Posts

It seems like MySQL does case-sensitive string comparisons?
You can easily ignore character case by changing your query to something like this:

$databaseItem= DataObject::get_one('Item' , 'LOWER(Title) = "' . $item. '"');

Also make sure $item is all lowercase (use strtolower).
Nevertheless you should check if you get any result, before accessing it. Just to prevent errors:

if($databaseItem){
   print($databaseItem->Title);
} else {
   print("no matching object found");
}

Avatar
jdi

22 February 2010 at 12:24pm Community Member, 34 Posts

Sweet.

That seems to have outlined where I'm going wrong. So I'm wondering if you can steer me in the right direction:

print ($item);

will return 'DataObjectSet'. I starting to think that I can't use $item->Title to retrieve the Title value from a DataObjectSet, how should I be going about getting it?

Go to Top