Filter/Search Multiple Data Objects as 1 list?

Silverstripe Version: 4


I have a site I am building, and let’s say I have DataObjects 1, 2, and 3. All belonging to different pages. I created a text filter (similar to the lessons example) and it works beautifully as long as I am only pulling in DataObject 1. When I attempt to merge all 3 DOs into an ArrayList the list loads fine, but I cannot filter it, any search results in no results?

Below is a link to the code in question.

Thanks for any help in advance :slight_smile:

Hi Ralph, I think if you replace ->merge() with ->push() it might start working :slight_smile:

Still fails on me. It’s very odd. I have updated the code in my original link.

Hi Ralph,

The :PartialMatch filter will only work when performing a database query, so that will need to happen before you merge the 3 lists into one. You’ll need to filter each of the 3 lists separately, something like:

$dataobject1 = $dataobject1->filter('Title:PartialMatch', $search)->sort('LastEdited', 'DESC');
$dataobject2 = $dataobject2->filter('Title:PartialMatch', $search)->sort('LastEdited', 'DESC');
$dataobject3 = $dataobject3->filter('Title:PartialMatch', $search)->sort('LastEdited', 'DESC');

I’ve also moved the sort here so that’s part of the database query as well. It’s much more efficient that way - once you create an ArrayList of the results, PHP will have to inspect each object to find the LastEdited date and then attempt to perform a sort in-memory.

Once you’ve filtered the objects, you can combine the 3 result sets into one list.

$fulllist = new ArrayList();

As soon as you create an ArrayList, you’re putting the database query results in-memory - which is why :PartialMatch won’t work after that point. It’s also worth pointing out that if you have a large number of results, you may hit PHP memory limits because of the number of items you’re loading at once.

1 Like

Great points Loz. Ralph I just wanted to clarify about when to use push vs merge. In your original code you were looping over a DataList and adding individual DataObjects to an ArrayList, so that is when you would use push. In your latest code I see you’re using push but you’re not looping over a DataList anymore, but trying to merge DataLists in to an ArrayList. So in that case you would use merge.