Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » $DataObject::get() Question

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

Page: 1 2
Go to End
Author Topic: 5527 Views
  • secuaz
    Avatar
    Community Member
    21 Posts

    Re: $DataObject::get() Question Link to this post

    Thanks very much for the answer Willr,

    ->where("Title LIKE '$searchString'") Didn't work however I used:

    $entries = BookstoreBook::get()
    ->filter(array(
    'Title:PartialMatch' => $searchString,
    'Description:PartialMatch' => $searchString
    ));
    $pagedList = new PaginatedList($entries);
    $pagedList->setPageLength(1);
    return $pagedList;

    Which returned the entries that matched the title. I also need it to match other fields not only title, but adding another key value pair to the associative array didn't work as the OR I expected:

    ->filter(array(
    'Title:PartialMatch' => $searchString,
    'Description:PartialMatch' => $searchString -> I expected adding this would make an OR but it only matches the Title...
    ));

    Any idea why it doesn't work????

    Thank youu!!!!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: $DataObject::get() Question Link to this post

    use filterAny() for OR rather than filter() which will be conjunctive.

  • secuaz
    Avatar
    Community Member
    21 Posts

    Re: $DataObject::get() Question Link to this post

    Hi thanks very much for the function name!!

    I followed the example from https://github.com/silverstripe/sapphire/pull/870/files on line 170....

    Am I doing something wrong?? Im using ss3.

    $entries = BookstoreBook::get()
    ->filterAny(array(
    'Title:PartialMatch' => $searchString,
    'Description:PartialMatch' => $searchString,
    'ISBN:PartialMatch' => $searchString
    ));

    I get the following error:

    [User Error] Uncaught Exception: Object->__call(): the method 'filterany' does not exist on 'DataList'

    Thank You in advancee!!!

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: $DataObject::get() Question Link to this post

    At the moment it seems filterAny() can only be found in the 3.1 branches, not in 3.0

  • secuaz
    Avatar
    Community Member
    21 Posts

    Re: $DataObject::get() Question Link to this post

    Thank you, I am using 3.05 that's why it doesn't work then,

    Any idea on how to implement the same idea on 3.05??

    I tried:
    $entries = BookstoreBook::get()->where("Title LIKE '$searchString'") ; But it doesn't work. I need to use: where title or description or isbn "LIKE" $searchstring

    Please help,

    Thank you!

  • secuaz
    Avatar
    Community Member
    21 Posts

    Re: $DataObject::get() Question Link to this post

    Hi all!
    After some pain I managed to use LIKE in a where statement.

    ->where("BookAuthor.Name LIKE '$searchString' OR BookstoreBook.Title LIKE '$searchString'")

    The problem is that "LIKE" doesn't partially match the fields; it does a total match.

    Does anybody know what could I use instead of "LIKE" to check if $searchstring is contained in a field???

    Thank You in advancee!!!

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: $DataObject::get() Question Link to this post

    LIKE is a mysql command, that works with wildcards: an underscore _ for a single character and a percentage character % for multiple characters. So searching for your searchstring as a partialmatch in a booktitle would be something like:

    "Bookstorebook.Title LIKE '%{$searchString}%'"

    I suppose SilverStripe will leave these charactes alone, so that should work? Mind: if you use a % at the beginning of the string, your mysql index cannot be used, so be carefull on large databases

  • secuaz
    Avatar
    Community Member
    21 Posts

    Re: $DataObject::get() Question Link to this post

    Thank you soooo much!!!!!
    It works perfectly!!!!

    $entries = BookstoreBook::get()
    ->where("BookAuthor.Name LIKE '%$searchString%' OR BookstoreBook.Title LIKE '%$searchString%' OR BookstoreBook.Description LIKE '%$searchString%' OR BookstoreBook.ISBN LIKE '%$searchString%'")

    PerfecT!!!

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