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.

General Questions /

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

UploadField breaks filename/url when attaching a file in ModelAdmin


Reply


6 Posts   908 Views

Avatar
gshegosh81

Community Member, 10 Posts

10 July 2012 at 11:32pm

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?

Avatar
gshegosh81

Community Member, 10 Posts

12 July 2012 at 12:46am

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?

Avatar
gshegosh81

Community Member, 10 Posts

13 July 2012 at 3:58am

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 :-)

Avatar
Hattori

Community Member, 20 Posts

22 July 2012 at 6:50am

Edited: 22/07/2012 8:03am

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.

Avatar
Bambii7

Community Member, 254 Posts

12 December 2012 at 11:16pm

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

Avatar
Ondawa

Community Member, 1 Post

27 December 2012 at 10:32am

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