1
votes

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?

1

1 Answers

2
votes

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.