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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Using ImageDOM in subfolders when using DataObject as Pages


Go to End


2 Posts   829 Views

Avatar
DesignCollective

Community Member, 66 Posts

17 February 2011 at 1:09pm

Edited: 17/02/2011 1:09pm

Hi guys, I am creating a ModelAdmin in which I will manage DataObjects, e.g. 'Project'. Each Project has a $has_many of Images that will be managed by an ImageDataObjectManager.

I would like, upon creating a Project, to have the set of images in its unique subfolder, as there could be hundreds of Projects in my ModelAdmin.

Now, when I create a new Project in ModelAdmin, none of the fields are set, so the IDOM's setUploadFolder method doesn't know $this->Folder, because it doesn't exist yet. How do I approach this? A few thoughts:

a) IDOM is present and visible before I save the Project. That means I can technically upload images before I've saved. I could make IDOM invisible until I've saved and then normally upload the images. By then, the Folder will be known to IDOM.

b) I could just run some kind of a function upon saving the Project object that will move all the Photos, if any, into the appropriate subfolder.

I'm not that good in SS to know what needs to be done in either of the two cases. Any help on this would be appreciated.


class Project extends DataObject {
	
	static $db = array(
                ...
		'Folder' => 'Varchar',
		...
	);
	
	static $has_many = array (
		'Photos' => 'Photo'
	);
	
	function getCMSFields() {

		$fields=parent::getCMSFields();
		...		
		$photoManager = new ImageDataObjectManager(
			$this, // Controller
			'Photos', // Source name
			'Image', // Source class
			'File', // File name on DataObject
			array(
				'Description'=>'Description'
			)
		);
		$photoManager->setUploadFolder($this->Folder); 
		$fields->addFieldToTab("Root.Images",$photoManager);						
		return $fields;
	}
}

Avatar
DesignCollective

Community Member, 66 Posts

17 February 2011 at 1:23pm

Edited: 17/02/2011 1:23pm

Ha, alright... I found this and it's probably the best solution).

I just used:

if($this->ID) {
  //any fields that need the record to be saved first e.g upload fields, ImageDOM etc.
}

Thanks!