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, Ed, biapar, Willr, Ingo, swaiba

UploadField breaks filename/url when attaching a file in ModelAdmin

Go to End

6 Posts   1296 Views


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:


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?


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?


Community Member, 10 Posts

13 July 2012 at 3:58am

I created a new ticket for this one if it gets some attention, perhaps someone would help me fix it :-)


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'


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

then run:

then logout and login again.


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


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.