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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Files can be attached once you have saved the record for the first time


Go to End


3 Posts   2384 Views

Avatar
ColdBlooded

Community Member, 4 Posts

4 June 2013 at 11:28am

Edited: 04/06/2013 11:29am

Hi there,

I'm having issues with SilverStripe backend image upload to a specific dataobject ID.
"Files can be attached once you have saved the record for the first time."

I want to attach the file before creating the record. I've tried using Uploadify but Uploadify has no support for SilverStripe 3. I've tried porting Uploadify for old SilverStripe version to SilverStripe 3 but that was very messy.

Screenshot:

Code example for this simple gallery module:

<?php

/*
 * Gallery & Images DataStructure with ModelAdmin enabled in the CMS.
 * ModelAdmin called: Gallery & Images
 */

class Gallery extends DataObject {
    static $db = array(
        "Name"=>"Varchar(255)",
        "DateTime"=>"SS_DateTime",
    );
    static $has_many = array(
        "GalleryImages" => "GalleryImages",
    );
}

class GalleryImages extends DataObject {
    static $db = array(
        "Name"=>"Varchar(255)",
        "Description"=>"Text",
        
    );
    static $has_one = array(
        "Gallery" => "Gallery",
        "galleryImage" => "Image", 
    );

 function getCMSFields() {
     
    $fields = parent::getCMSFields();
    $fields->removeByName("galleryImage");
    $fields->addFieldToTab("Root.Main", $if = new UploadField('galleryImage','Gallery Images'));
    $if->setConfig('allowedMaxFileNumber', 500)->setFolderName('gallery/gallaryimg');
    $if->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
    return $fields;
    
}
}
class GalleryImagesModelAdmin extends ModelAdmin {
    public static $managed_models = array('Gallery','GalleryImages'); 
    static $url_segment = 'GalleryImages';
    static $menu_title = 'Gallery & Images';
}

?>

Any pointers would be greatly appreciated.
Thanks.

Avatar
Willr

Forum Moderator, 5523 Posts

4 June 2013 at 5:07pm

You've got to save the record before you add it (like it says). A patch has added support for uploads

https://github.com/silverstripe/silverstripe-framework/pull/1862

Avatar
ColdBlooded

Community Member, 4 Posts

6 June 2013 at 9:18am

Edited: 06/06/2013 10:28am

Thank you for the pointer! Couldn't find that when I went digging for the fix for this.