$owns not working with DataObjects?

Silverstripe Version: 4.0.3

I need to publish an Image on upload to a data object.

As i wrote in the lessons section you have to declare ownership. Does that only work if i extend Page? I created a Gist with my class. It seems i don´t get the concept of $owns.

https://gist.github.com/Greg808/0f2b0bd0e131d89a1ea28734d2718a9b

2 Likes

Yeah, that doesn’t work with SilverStripe 4.0.3… should be fixed once 4.1 lands.
The problem is, that your DataObject is not versioned. You could apply the Versioned extension to it, or publish the file in your onAfterWrite method:

protected function onAfterWrite()
{
    parent::onAfterWrite();
    // Do this for all your assets that should be published…
    if ($this->owner->PreviewImageID) {
        $this->owner->PreviewImage()->publishSingle();
    }
}
3 Likes