3060 Posts in 864 Topics by 646 members
| Go to End | Next > | |
| Author | Topic: | 1576 Views |
-
Force ignore case

16 February 2010 at 10:43am
Hey Guys,
Long time no see. Here's a question which I would love some assistance with
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...
-
Re: Force ignore case

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));
-
Re: Force ignore case

16 February 2010 at 11:42am
But if $item is 'iron man' then why are you calling $item->Title?
-
Re: Force ignore case

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. -
Re: Force ignore case

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.
-
Re: Force ignore case

18 February 2010 at 3:26pm Last edited: 18 February 2010 4:45pm
Do a var dump on $databaseItem and check if it is an object. Is your MySQL running in ANSI mode?
-
Re: Force ignore case

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");
} -
Re: Force ignore case

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?
| 1576 Views | ||
| Go to Top | Next > |



