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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Find all DataObjects that extend a particular DataExtension


Go to End


4 Posts   1211 Views

Avatar
Salted Herring

Community Member, 2 Posts

7 August 2014 at 1:43pm

Is it possible to search for DataObjects that extend a particular extension?

I'm trying to make a build task that will populate some fields based on whether they extend a custom extension. We don't appear to be able to use DataObject::get() - so is there a generic way of filtering DataObjects, or do I have to specify every DataObject & query them that way?

Avatar
Kirk

Community Member, 67 Posts

7 August 2014 at 2:02pm

Edited: 07/08/2014 2:02pm

You should be able to use the 2 native php functions get_declared_classes and is_subclass_of

$i = 0;
foreach (get_declared_classes() as $class) {
        if (is_subclass_of($class , 'DataObject')) {
        echo "<p>Class $class</p>";
                $i++;
        }
}
echo "<p>$i classes extend from DataObject</p>";

Avatar
Salted Herring

Community Member, 2 Posts

7 August 2014 at 5:52pm

Thanks Kirk.

I'm not sure whether this is because I'm running this inside a build task, but consequently I only get Silverstripe's defined classes & none of my own. Do you think it's possibly due to the way that SS lazy loads the ORM & hasn't encountered one of these dataobjects & hence they aren't available?

Avatar
Kirk

Community Member, 67 Posts

8 August 2014 at 9:17am

I did initially think it could be to do with how the order of the build tasks runs.
If you add a your own model you need to run a dev/build for the DB table to be created and you can see this on the output so it must get around to loading all classes eventually.

However looking at framework/model/DatabaseAdmin.php the build task there uses the following line

                // Get all our classes
                SS_ClassLoader::instance()->getManifest()->regenerate();

It might be an idea to look at SS_ClassLoader/manifests or replace the the get_declared_classes with a call to SS_ClassLoader::instance()->getManifest()->regenerate()