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   2022 Views

Avatar
jdi

Community Member, 34 Posts

16 February 2010 at 10:43am

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

Community Member, 528 Posts

16 February 2010 at 11:16am

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

Community Member, 89 Posts

16 February 2010 at 11:42am

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

Avatar
banal

Community Member, 901 Posts

16 February 2010 at 11:51am

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

Community Member, 34 Posts

18 February 2010 at 3:11pm

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

Community Member, 89 Posts

18 February 2010 at 3:26pm

Edited: 18/02/2010 4:45pm

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

Avatar
banal

Community Member, 901 Posts

18 February 2010 at 8:49pm

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

Community Member, 34 Posts

22 February 2010 at 12:24pm

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