csy_dot_io
csy_dot_io

Reputation: 1199

Display dataobjects from different classes in one list - SilverStripe 3.4

I'd like to create a loopable list of dataobjects with different classes.

Something like that, but functioning

DataObject::get()->filter('ClassName, ['MyClass', 'MyOtherClass']);

Is there a way to achieve that without subclassing?

Upvotes: 1

Views: 57

Answers (1)

muskie9
muskie9

Reputation: 476

csy_dot_io you can create an ArrayList with both object lists:

public function getCombinedList()
{

    $list = ArrayList::create();
    $pushToList = function($object) use (&$list)
    {
        $list->push($object);
    };

    MyClass::get()->each($pushToList);
    MyOtherClass::get()->each($pushToList);

    return $list;

}

If you're looking to manage multiple objects in one GridField then you could check out gridfieldextensions, specifically the GridFieldAddNewMultiClass component.

Upvotes: 2

Related Questions