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

Preview: DataObjectManager module


Go to End


379 Posts   95924 Views

Avatar
LesC

Community Member, 70 Posts

27 February 2009 at 11:51pm

This looks like a pretty powerful module, but I've got one question:

How do I keep uploaded resources related to just the page they're uploaded to? I can see this has been done on the demo site, but can't see where the relationship is defined in the example code.

When I upload a file, all other pages that have that class can also see the file in the related files list.

Code: http://pastie.org/401950

Can anyone point me in the right direction?

Cheers

L

Avatar
Carbon Crayon

Community Member, 598 Posts

27 February 2009 at 11:58pm

Edited: 28/02/2009 12:56am

Hi Les

I am pretty sure you can just add a filter clause in the same way as the complex table field:

$manager = new FileDataObjectManager(
$this, // Controller
'FileResources', // Source name
'FileResource', // Source class
'Attachment', // File name on DataObject ( Resource::$has_one = array('Attachment' => 'File') )
array(
'DisplayTitle' => 'DisplayTitle'
), // Headings
'getCMSFields_forPopup', // Detail fields (function name or FieldSet object)
"ServicePageID = $this->ID" //Filter clause ******HERE****** make sure to add the coma above too
// Sort clause
// Join clause
);

Avatar
LesC

Community Member, 70 Posts

28 February 2009 at 12:20am

Wow, that was a quick response! Thank you Aram.

I'm not sure exactly how to implement what you suggested, as it immediately throws a PHP error in my server logs, so I've done a quick search and improvised:

$manager = new FileDataObjectManager(
			$this, // Controller
			'FileResources', // Source name
			'FileResource', // Source class
			'Attachment', // File name on DataObject ( Resource::$has_one = array('Attachment' => 'File') )
			array(
				'DisplayTitle' => 'DisplayTitle'
			), // Headings 
			'getCMSFields_forPopup', // Detail fields (function name or FieldSet object)
			$filter = 'ServicesPageID = '.$this->ID// Filter clause
			// Sort clause
			// Join clause
		);

Now it's complaining that there's and "Unknown column 'ServicesPageID' in 'where clause'" - is this something that I need to define elsewhere (maybe in the FileResource class)?

Sorry, I'm having a Friday morning brain problem today!!

Avatar
LesC

Community Member, 70 Posts

28 February 2009 at 12:31am

Ahahaha!! A cupt of tea and fits into place...

It turns out that I'd declared a has_one relationship in the FileResource class, but had named it Page, not ServicesPage - a simple change in the FileResource class to:

static $has_one = array (
		'Attachment' => 'File',
		'ServicesPage' => 'ServicesPage'
	);

and a quick rebuild of the DB and all is working.

Now, my next question is - do I need to create a Resource class for each of the page types that I want to display this control on, or is there an easy way for me to set the filter on the main page ID? Can I do it through the SiteTree table at all?

Avatar
Carbon Crayon

Community Member, 598 Posts

28 February 2009 at 12:38am

I think you should be able to just add the page types to the has one relation ship, so you will have a number of has_one's but only one will be used at a time. Not sure if there is a better way to do it though

Avatar
LesC

Community Member, 70 Posts

28 February 2009 at 12:46am

Thanks for the nudge Aram, that works perfectly. :)

Avatar
LesC

Community Member, 70 Posts

28 February 2009 at 2:08am

Edited: 28/02/2009 3:33am

Okey dokey, two more quick questions:

1. How do you define the default upload folder for the FileDataObjectManager module?
I don't want my users to be uploading to the root of the Upload folder, as it'll make attaching files and images very messy!

2. And, is there a way to store or display the file properties such as filesize and filetype?
I can see that the control for editing the asset shows both, but can't see how I call this in the UI.

Cheers

L

Avatar
UncleCheese

Forum Moderator, 4102 Posts

28 February 2009 at 3:43am

1) This is actually in production right now. I've had a number of requests for it. Check out the svn later today (it's Friday morning here). The implementation is $manager->allowUploadFolderSelection();

2) File size and type are stored automatically in the File table. Use $Size and $Type (I believe). In the example code for "Resource" that would look like this:

$Attachment.Size
$Attachment.Type

(might be FileType, or something like that. I forget the exact name of the property)

Go to Top