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.

General Questions

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

$DataObject::get() Question


Reply

16 Posts   5726 Views

Avatar
secuaz

3 May 2013 at 11:15pm (Last edited: 3 May 2013 11:15pm), Community Member, 21 Posts

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

Avatar
Willr

5 May 2013 at 4:25pm Forum Moderator, 5511 Posts

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

Avatar
secuaz

8 May 2013 at 11:34pm (Last edited: 8 May 2013 11:37pm), Community Member, 21 Posts

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

:)

Avatar
martimiz

9 May 2013 at 12:01am (Last edited: 9 May 2013 12:02am), Forum Moderator, 1095 Posts

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

Avatar
secuaz

9 May 2013 at 12:44am Community Member, 21 Posts

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!

Avatar
secuaz

9 May 2013 at 2:13am Community Member, 21 Posts

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

Avatar
martimiz

9 May 2013 at 3:37am Forum Moderator, 1095 Posts

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

Avatar
secuaz

9 May 2013 at 4:13am Community Member, 21 Posts

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