问题
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;
回答1:
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.
来源:https://stackoverflow.com/questions/47392211/silverstripe-file-relation-in-modeladmin-doesnt-publish