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

Customising the CMS /

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

managing has_many 'File' relation in ModelAdmin

Go to End

4 Posts   2947 Views


Community Member, 2 Posts

29 October 2012 at 7:10am


I have a has_many 'File' relation set on my Model. When i want to edit this relation with AdminModel I can link existing or add new File (in my case Datoteka). But clicking the add new File doesn't show me the uploadFile field but rather the form for already uploaded files which is empty (doh, no file to edit).

What do i have to do to get the upload field when I want to add a new File to my database. I'm posting the image of the form which opens (but i want the uploadfile field there instead).

Sorry about the language on the picture.

Carbon Crayon

Community Member, 598 Posts

1 November 2012 at 9:22pm

Hi zrelieskim,

You need to use a field that can handle multiple file relations, like Uploadify could in 2.4 (not sure if there are plans to update it to 3.0).

The other option you have is to create a has_many to a custom DataObject which has_one file. Not ideal, but does have the advantage of allowing you to add custom metadata to them as you upload.

Aram - SilverStripe Tutorials, tips and other bits.


Community Member, 2 Posts

1 November 2012 at 10:03pm

Edited: 01/11/2012 10:05pm

Thanks for your reply Aram.

I've made some progress, but I'm stuck again. First, I realised that has_many relation only works if the other Object has 'has_one' defined, so I changed the relation to many_many.

Then I decided not to subclass 'File' DataObject but instead create a DataExtension. With It's method updateCMSFields I can control which fields are displayed in the create new 'File' Form.

Then I tried adding the new UploadField to this Form (via the updateCMSFields), but here comes the problem. UploadField requires a relation to the parent Object which i have no idea how to get from the DataExtension class.

class GradivoFile extends DataExtension {
	public static $db = array();

	public function updateCMSFields(FieldList $fields) {
			new UploadField("Datoteka", "Datoteka", $this->owner)


And in my_config.php i have:
Object::add_extension('File', 'GradivoFile');

The problem with the code is that $this->owner returns a 'File' object instead the Object with many_many defined (in my case Gradivo, which means Material in Slovene). So when i try uploading file with the form there is an error.

I've tried to overcome this problem but i can't seem to understand how to get the data from silverstripe controllers. The only way i can think of is from the URL but this sounds a bit to lame, so I didn't even try it:
URL: .../admin/gradiva/Gradivo/EditForm/field/Gradivo/item/1/ItemEditForm/field/Datoteka/item/new

Carbon Crayon

Community Member, 598 Posts

1 November 2012 at 10:13pm

When using Data Extension you can add relationships in the same way you would on a normal class:

However I would suggest extending it and adding a has_one to the extended class to keep things contained rather than applying to every file. There is an exampel of this on the Uploadify docs under 'Multiple Files' header:

Aram - SilverStripe Tutorials, tips and other bits.