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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

$DataObject::get() Question


Go to End


16 Posts   17089 Views

Avatar
secuaz

Community Member, 24 Posts

3 May 2013 at 11:15pm

Edited: 03/05/2013 11:15pm

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

Forum Moderator, 5523 Posts

5 May 2013 at 4:25pm

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

Avatar
secuaz

Community Member, 24 Posts

8 May 2013 at 11:34pm

Edited: 08/05/2013 11:37pm

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

Forum Moderator, 1391 Posts

9 May 2013 at 12:01am

Edited: 09/05/2013 12:02am

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

Avatar
secuaz

Community Member, 24 Posts

9 May 2013 at 12:44am

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

Community Member, 24 Posts

9 May 2013 at 2:13am

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

Forum Moderator, 1391 Posts

9 May 2013 at 3:37am

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

Community Member, 24 Posts

9 May 2013 at 4:13am

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

Go to Top