Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Files can be attached once you have saved the record for the first time

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 854 Views
  • ColdBlooded
    Avatar
    Community Member
    4 Posts

    Files can be attached once you have saved the record for the first time Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Files can be attached once you have saved the record for the first time Link to this post

    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

  • ColdBlooded
    Avatar
    Community Member
    4 Posts

    Re: Files can be attached once you have saved the record for the first time Link to this post

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

    854 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.