Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Force ignore case

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 1937 Views
  • jdi
    Avatar
    Community Member
    34 Posts

    Force ignore case Link to this post

    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...

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Force ignore case Link to this post

    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));

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: Force ignore case Link to this post

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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Force ignore case Link to this post

    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.

  • jdi
    Avatar
    Community Member
    34 Posts

    Re: Force ignore case Link to this post

    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.

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: Force ignore case Link to this post

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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Force ignore case Link to this post

    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");
    }

  • jdi
    Avatar
    Community Member
    34 Posts

    Re: Force ignore case Link to this post

    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?

    1937 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.