How to automaticaly publish files uploaded to a dataobject in Silverstripe model admin

Your dataobject needs to extend the Versioned extension. Pages already have this in the SiteTree object.

Class Item extends DataObject
{
    private static $has_one = [
        'ItemImage' => Image::Class,
        'Catalog' => 'Catalog'
    ];

    private static $owns = [
        'ItemImage'
    ];

    private static $extensions = [
        Versioned::class . '.versioned'
    ];
}

Edit

The above doesn't actually work for a ModelAdmin, only for objects related to an object that already is 'Versioned' (like SiteTree).
If you want to this from a ModelAdmin you could add the following:

private static $versioned_gridfield_extensions = true;

Which will create a few buttons in your ModelAdmin. After you click publish, the File will publish too.


This is currently discussed on GitHub on multiple repositories.

The solution at the moment, is either publish the images manually in onAfterWrite, or version your DataObject, preferably via YML:

My\Data\Object
  extensions:
    - Versioned