3

Im using SilverStripe4 and the ModelAdmin to manage DataObjects.

The DataObject has a has_one on File. Everything works so far but on frontend controller the File relation has an empty object.

I see that the file is not in the File_Live table, so i guess its not published and therefor its not found on the frontend controller.

How can i publish File relations from the ModelAdmin? Basically when a file is uploaded it should be automatically published.

I guess if i use versioned DataObjects i would still need something like this: https://github.com/drzax/silverstripe-bits/tree/master/VersionedModelAdmin
to have publish mechanism on ModelAdmin.

Or is there something builtin in SS4? Would this cascade down to File relations as well?

Edit: regarding versioned DataObjects there is a built in publish button in SS4 just use:

private static $extensions = [
    Versioned::class,
];

private static $versioned_gridfield_extensions = true;
ivoba
  • 5,780
  • 5
  • 48
  • 55

1 Answers1

4

You can add the following to your DataObject:

private static $owns = ['FileRelationName'];

Example with a relation:

private static $has_one = ['File' => File::class];
private static $owns = ['File'];

Any related object that is being declared as "owned" in this way will be published with the DataObject itself.

bummzack
  • 5,805
  • 1
  • 26
  • 45
  • Does this work only when the owning DataObject is versioned? – ivoba Nov 20 '17 at 12:51
  • @ivoba AFAIK it also works if your DataObject isn't versioned – bummzack Nov 20 '17 at 12:52
  • reading this, I remember I've stumbled over it here: https://github.com/silverstripe/silverstripe-framework/issues/7359 It's a 4.1 milestone, but you bothy say it works now with 4.0 – munomono Nov 20 '17 at 17:30
  • 1
    @munomono i need to add: i added versioning to my dataobjects. so i cant really say it works for non versionened dataobjects. – ivoba Nov 21 '17 at 07:44