Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » UploadField breaks filename/url when attaching a file in ModelAdmin

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

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

Page: 1
Go to End
Author Topic: 732 Views
  • gshegosh81
    Avatar
    Community Member
    10 Posts

    UploadField breaks filename/url when attaching a file in ModelAdmin Link to this post

    So, I have this DataObject:

    class Product extends DataObject {
       static $db = array(
          'Name' => 'Varchar',
          'Price' => 'Int',
          'InitialQuantity' => 'Int',
          'Content' => 'HTMLText',
       );
    // (...)
       static $has_many = array(
          'Picture' => 'Image'
       );
    // (...)

    and getCMSFields like this:

       public function getCMSFields() {
          $fields = parent::getCMSFields();
          $field = new UploadField('Picture', 'Zdjęcie');
          $field->allowedExtensions = array('jpg', 'gif', 'png');
          $fields->addFieldToTab('Root.Main', $field);
          return $fields;
       }

    The ModelAdmin for this is a default one. Now, when I upload an image file from my computer, it ends up at /assets/Upload/file.jpg on the server and it is all right. But the URL shown in ModelAdmin is /assets/file.jpg and after the upload completes, the thumbnail disappears.

    I've debugged through it and found out, that in UploadField.upload method, around line 506 is a line:

    $this->attachFile($file);

    Before it, everything's fine, after it the filename and URL is broken (e.g. Upload part of the path is missing, just /assets/file.jpg is there). Going deeper in SilverStripe code, I've found out that it is broken at HasManyList.add method, where around line 55 is a line:

    $item->$fk = $this->foreignID;

    Before it everything's fine, after it the filename and URL is broken.

    It is too magic code for me to fix it, would anyone be able to offer help with this? Am I doing something wrong or is it a bug?

  • gshegosh81
    Avatar
    Community Member
    10 Posts

    Re: UploadField breaks filename/url when attaching a file in ModelAdmin Link to this post

    If this is a wrong forum, could someone please point me in a better direction? I would fix this - probable - bug if I understood or could read about the magic that goes in the HasManyList class... A developer forum maybe?

  • gshegosh81
    Avatar
    Community Member
    10 Posts

    Re: UploadField breaks filename/url when attaching a file in ModelAdmin Link to this post

    I created a new ticket for this one http://open.silverstripe.org/ticket/7655 if it gets some attention, perhaps someone would help me fix it

  • Hattori
    Avatar
    Community Member
    18 Posts

    Re: UploadField breaks filename/url when attaching a file in ModelAdmin Link to this post

    So i found solution, as oetiker mentioned, just change:

    static $has_many = array(
       'Images' => 'Image'
    );

    to

    static $many_many = array(
       'Images' => 'Image'
    );

    then run:
    /dev/build?flush=all
    /admin/pages?flush=all

    then logout and login again.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: UploadField breaks filename/url when attaching a file in ModelAdmin Link to this post

    Yip I had the same issue. Changed the relationship from $has_many to $many_many and it works.
    I don't quite know why it can't be has_many, I guess cause once it's uploaded it could belong to other items....

  • Ondawa
    Avatar
    Community Member
    1 Post

    Re: UploadField breaks filename/url when attaching a file in ModelAdmin Link to this post

    I'm so glad I found this solution. I had the same issue and it was driving me crazy.

    732 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.