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 forum.silverstripe.org 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.

Form Questions /

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

FileAttachmentField saving but not attaching files


Go to End


7 Posts   5441 Views

Avatar
DesignCollective

Community Member, 66 Posts

29 May 2015 at 10:34pm

Got a Dataobject with many_many attachments and have set the appropriate has_many relationships (I think so?)

class VideoSubmission extends DataObject {

private static $many_many = array(
    'Attachments' => 'File'
  );

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fileField = UploadField::create('Attachments', 'Upload some  files');
   return $fields;
}

// Add a data extension for file
class SubmissionFileExtension extends DataExtension{
    private static $belongs_many_many = array(
        'VideoSubmissions' => 'VideoSubmission'
    );
}
File::add_extension('SubmissionFileExtension');

This works. But if I replace the fileUploadField above with:

$fileField = FileAttachmentField::create('Attachments', 'Upload some  files')
      ->setView('grid');

Then files will upload but not attach.

I'm able to see the files added with UploadField (previously), I can detach them but can't add anything new.
I'm on the most recent composer installed ss (today).

My composer.json

{
	"name": "silverstripe/installer",
	"description": "The SilverStripe Framework Installer",
	"require": {
		"php": ">=5.3.2",
		"silverstripe/cms": "3.1.13",
		"silverstripe/framework": "3.1.13",
		"silverstripe-themes/simple": "*",
		"silverstripe/memberprofiles": "dev-master",
		"rywa/silverstripe-foundation-forms": "~1.0@dev",
		"burnbright/silverstripe-externalurlfield": "*@stable",
		"silverstripe-australia/gridfieldextensions": "dev-master",
		"unclecheese/dropzone": "dev-master"
	},
	"require-dev": {
		"phpunit/PHPUnit": "~3.7@stable"
	},
	"config": {
		"process-timeout": 600
	},
	"minimum-stability": "dev"
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

29 May 2015 at 11:37pm

Edited: 29/05/2015 11:38pm

Looks right. Do you need the file extension? I've confirmed that this works:

class TestFilePage extends Page {

	private static $many_many = array (
		'Files' => 'File'
	);


	public function getCMSFields() {
		$f = parent::getCMSFields();
		$f->addFieldToTab('Root.Upload',FileAttachmentField::create('Files'));

		return $f;
	}
}

Also, `add_extension()` is kind of old school. You'll want to move that to a definition in the config yaml.

Avatar
DesignCollective

Community Member, 66 Posts

30 May 2015 at 5:02am

Hm... I'm not attaching to a page, I'm trying to attach to a DataObject and the files won't upload in the CMS (the frontend behaves the same).

What I do:
- drag in the files (I see the files appear in the field, ready to load)
- the progress bars remain at 0 even if I set ->setAutoProcessQueue to true or false. I noticed that when false, clicking the Upload Files button doesn't trigger the upload either. I wonder if it's because I'm on MAMP? (at the same time, UploadField does work)
E.g.
https://www.dropbox.com/s/fm8jj6z88deyvmt/Screenshot%202015-05-29%2009.51.37.png?dl=0

What I did notice, in the network, I'm getting an "undefined" / cancelled GET request right after I add files into the field:
https://www.dropbox.com/s/tayikbws833zbe2/Screenshot%202015-05-29%2009.57.29.png?dl=0

Something's up for sure...

Here's my whole class, if you want to test it:

<?php

class VideoSubmission extends DataObject {

  private static $db = array(
    'Title' => 'Varchar(255)',
    'Text' => 'Text',
    'City' => 'Varchar(255)',
    'State' => 'Varchar(20)',
    'ZipCode' => 'Varchar(20)'
  );

  private static $has_one = array(
    'Member' => 'Member'
  );

  private static $many_many = array(
    'Files' => 'File'
  );

  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fileField = FileAttachmentField::create('Files', 'Upload some files')
      ->setView('grid');
      ->setAutoProcessQueue(true) // do not upload files until user clicks an upload button
      ->setAcceptedFiles(array('.jpg', '.jpeg', '.pdf','.doc','.docx', '.mov', '.mp4', '.gp4', '.avi', '.mp3', '.wav'));
    $fields->addFieldsToTab("Root.Main",
      array(
        new TextField("Title", "Titulo"),
        new TextareaField("Text","Descripcion"),
        new TextField("City", "Ciudad"),
        new DropdownField("State", _t("MEMBERS.State","Estado"), Helpers::getStateDropDown()),
        new TextField("ZipCode", "Codigo Postal"),
        $fileField
      )
    );
    return $fields;
  }

}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

30 May 2015 at 12:10pm

Edited: 30/05/2015 12:12pm

So you're managing this in ModelAdmin, then? I can confirm that this works:

class TestFileDataObject extends DataObject {

	private static $many_many = array (
		'Files' => 'File'
	);


	public function getCMSFields() {
		$fields = FieldList::create(TabSet::create('Root'));
		$fields->addFieldToTab('Root.Main', FileAttachmentField::create('Files'));

		return $fields;
	}
}

class TestAdmin extends ModelAdmin {

	private static $managed_models = array (
		'TestFileDataObject'
	);

	private static $menu_title = 'Test';

	private static $url_segment = 'test';
}

The "undefined" path is obviously the issue. Can you inspect the field and check the data-config setting? It should look something like this:

data-config="{"url":"admin\/test\/TestFileDataObject\/EditForm\/field\/TestFileDataObject\/item\/1\/ItemEditForm\/field\/Files\/upload","maxFilesize":2,"uploadMultiple":true,"clickable":".dropzone-select","params":{"SecurityID":"6688d3e8e524ba62a22fe1db0c184ac40c7d64e8"},"thumbnailsDir":"dropzone\/images\/file-icons\/64px","thumbnailWidth":64,"thumbnailHeight":64,"urlSelectDialog":"admin\/test\/TestFileDataObject\/EditForm\/field\/TestFileDataObject\/item\/1\/ItemEditForm\/field\/Files\/select"}"

I assume your "url" setting contains no value?

Avatar
DesignCollective

Community Member, 66 Posts

30 May 2015 at 2:17pm

Edited: 30/05/2015 2:31pm

Nope, the config is good and the url is there all clean. For the heck of it, I used your test class and it's also failing to upload files which I thought was really weird. I removed some composer dependencies to see if any clashed, I went from 3.1.13 to 3.1.14.x-dev to check it out. No changes.

Config looks good:

{"url":"admin\/test\/TestFileDataObject\/EditForm\/field\/TestFileDataObject\/item\/new\/ItemEditForm\/field\/Files\/upload","maxFilesize":32,"uploadMultiple":true,"clickable":".dropzone-select","params":{"SecurityID":"51137d84be772294c952ae9f8407e5829294fa70"},"thumbnailsDir":"dropzone\/images\/file-icons\/64px","thumbnailWidth":64,"thumbnailHeight":64,"urlSelectDialog":"admin\/test\/TestFileDataObject\/EditForm\/field\/TestFileDataObject\/item\/new\/ItemEditForm\/field\/Files\/select"}

I actually thought it may have to do with a DataObject's getURLPrefix() but all this looks okay to me (as much as I can understand :)

The only clue I have so far is this: I noticed the growl-y error in CMS when creating a new TestFileDataObject in ModelAdmin and clicking "Create" after dragging in some files. The log states:

[30-May-2015 04:10:56] Warning at framework/model/UnsavedRelationList.php line 148: Invalid argument supplied for foreach() (http://itestigo.dev/admin/submissions/VideoSubmission/EditForm/field/VideoSubmission/item/new/ItemEditForm

Once a TestFileDataObject has an ID, the errors disappear but the behavior remains (no way to upload & attach / attaching from file library works).

The method from UnsavedRelationList.php is:

/**
* Add a number of items to the relation.
* 
* @param array $items Items to add, as either DataObjects or IDs.
* @return DataList
*/
public function addMany($items) {
	foreach($items as $item) {
		$this->add($item);
	}
	return $this;
}

$items (from wherever they are passed to this) are null at the point (caught it to see), hence I guess the error.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

2 June 2015 at 11:05am

I had another person report this, but I still can't replicate. Might want to follow along here and add your input. https://github.com/unclecheese/silverstripe-dropzone

Avatar
UncleCheese

Forum Moderator, 4102 Posts

3 June 2015 at 12:13am