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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Preview: DataObjectManager module


Reply

379 Posts   61613 Views

Avatar
LesC

27 February 2009 at 11:51pm Community Member, 70 Posts

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: [url]http://pastie.org/401950[/url]

Can anyone point me in the right direction?

Cheers

L

Avatar
Carbon Crayon

27 February 2009 at 11:58pm (Last edited: 28 February 2009 12:56am), Community Member, 598 Posts

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

28 February 2009 at 12:20am Community Member, 70 Posts

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

28 February 2009 at 12:31am Community Member, 70 Posts

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

28 February 2009 at 12:38am Community Member, 598 Posts

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

28 February 2009 at 12:46am Community Member, 70 Posts

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

Avatar
LesC

28 February 2009 at 2:08am (Last edited: 28 February 2009 3:33am), Community Member, 70 Posts

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

28 February 2009 at 3:43am 4085 Posts

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)