Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverstripe merge GroupedLists

I'm trying to return a single GroupedList which holds information from both pages and dataobjects. Is there an easy way to merge the two lists together?

public function getGroupedContent()
{
    $dataobjects = GroupedList::create(FileNetObject::get());
    $pages = GroupedList::create($this->Children());

    $result = ??;


    return $result;
}

The ArrayList merge fails as does a standard array_merge - Would I be best merging the results from the queries together before putting it into a single GroupedList?

like image 348
Chris Turner Avatar asked Nov 09 '22 07:11

Chris Turner


1 Answers

Without knowing more about your situation, the simplest way to do this is:

return array_merge($dataobjects->toArray(), $pages->toArray());

To display the above in a template you would of course need to wrap that in another ArrayList.

Updated: If you want to group the lists (which of course is why you were using a GroupedList in the first place) you would need to do it before merging the arrays. More like:

return array_merge($dataobjects->GroupBy('Author')->toArray(), $page->GroupBy('Author')->toArray());
like image 71
Mark Guinn Avatar answered Nov 30 '22 15:11

Mark Guinn