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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

SOLVED: UploadField in SilverStripe 4 (alpha 7)


Go to End


2 Posts   1719 Views

Avatar
martimiz

Forum Moderator, 1391 Posts

14 May 2017 at 2:09am

Edited: 14/05/2017 2:12am

I'm trying to add an UploadField to to a PageType to store a related File. Obviously doing something stupid, but what :(

use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;

class FilePage extends Page
{
    private static $has_one = array(
        "FileObject" => "File"
    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Main',
            UploadField::create('FileObject', 'My File')
        );
        return $fields;
    }

Generates: Uncaught InvalidArgumentException: has_one relation FilePage.FileObject references class File which doesn't exist.

Same happens with Image. Although both classes exist in the given namespace...

Avatar
martimiz

Forum Moderator, 1391 Posts

14 May 2017 at 2:38am

OK - Solved 5 minutes after this post... It works when you:

    private static $has_one = array(
        "FileObject" => "SilverStripe\Assets\File"
    );

or

    private static $has_one = array(
        "FileObject" => File::class
    );

(or also)

    private static $has_one = array(
        "FileObject" => "SilverStripe\\Assets\\File"
    );

use SilverStripe\Assets\File doesn't work, because obviously other classes that handle the relation, don't know about the namespace... Don't know which version is the preferred, as they all are used throughout core...