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   95933 Views

Avatar
vstrazz

Community Member, 63 Posts

9 April 2009 at 6:58am

I'm trying to add DataObjectManager to UserDefinedForm to help with sorting submissions, and exporting. ( for instance for mailing lists ). I did get it to add into UserDefinedForm, it just wont delete or edit or export lololol

Avatar
Amir Mostofi

Community Member, 59 Posts

9 April 2009 at 8:27am

To Hu, UncleCheese or anyone who could help.

The issue I have is what ntessore mentioned back on page 6 of this topic. When filedataobjectmanager is used on page class "Page" for instance it all works fine and there's no need for a filter clause or setParentClass. However, when you subclass Page, the subclassed pages show all the managed files. You can circumvent this by using a filter clause. However, with or without the filter clause, you cannot add any managed files to the subclassed pages. Basically, when you try to add a file to a page which is a subclass of Page, the added file does not stick to that page. Its PageID remains 0.
So the issue is always related to subclasses of a page type, not the page type itself. I'm not sure how ntessore resolved this issue. He mentioned using setParentClass, but I'm not sure exactly how and where I should use this, because my attempts at setParentClass have not worked.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 April 2009 at 8:48am

You do have to use setParentClass. That's what it's for. You have to do the same thing on a ComplexTableField. I've tested it, and the same thing happens.

Avatar
Amir Mostofi

Community Member, 59 Posts

9 April 2009 at 9:17am

Edited: 09/04/2009 9:18am

Thank you UncleCheese!
The problem I have is I guess not knowing exactly where to apply the setparentClass. Would you kindly assist? I really want to get the hang of this module as it's by far the most useful module.

Below are the files in question. Adding ImageAttachments does not stick to ResourcePage types which is a subclass of Page. And I have to use filter clause "PageID = {$this->ID}" in Page.php to prevent all added ImageAttachments to appear under the ResourcePage types (even ones from other Pages). Thank you in advance.

ImageAttachment.php
----------------
<?php
class ImageAttachment extends DataObject
{
static $db = array (
'Name' => 'Text',
'URL' => 'Text'
);

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

public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Name'),
new TextField('URL'),
new FileIFrameField('Attachment')
);
}
}
?>
----------------

Page.php
----------------
<?php

class Page extends SiteTree {

public static $db = array(
);

public static $has_one = array(
);

static $has_many = array (
'ImageAttachments' => 'ImageAttachment'
);

public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new FileDataObjectManager(
$this, // Controller
'ImageAttachments', // Source name
'ImageAttachment', // Source class
'Attachment', // File name on DataObject
array(
'Name' => 'Name',
'URL' => 'URL'
), // Headings
'getCMSFields_forPopup', // Detail fields (function name or FieldSet object)
"PageID = {$this->ID}" // Filter clause
// Sort clause
// Join clause
);

// If undefined, all types are allowed. Pass with or without a leading "."
$manager->setAllowedFileTypes(array('jpg','gif','swf'));

// Label for the upload button in the popup
$manager->setBrowseButtonText("Upload (JPG, GIF or SWF)");

// In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
$manager->setGridLabelField('Name');

// Plural form of the objects being managed. Used on the "Add" button.
// If left out, this defaults to [MyObjectName]s
$manager->setPluralTitle('Images');

$f->addFieldToTab("Root.Content.Images", $manager);

return $f;
}
}

class Page_Controller extends ContentController {
...
}

?>
----------------

ResourcePage.php
----------------
<?php
class ResourcePage extends Page
{
static $has_many = array (
'Resources' => 'Resource'
);

public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new FileDataObjectManager(
$this, // Controller
'Resources', // Source name
'Resource', // Source class
'Attachment', // File name on DataObject
array(
'Name' => 'Name',
'Description' => 'Description',
'Category' => 'Category'
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);

$manager->setFilter(
'Category', // Name of field to filter
'Filter by Category', // Label for filter
singleton('Resource')->dbObject('Category')->enumValues() // Map for filter (could be $dataObject->toDropdownMap(), e.g.)
);

// If undefined, all types are allowed. Pass with or without a leading "."
$manager->setAllowedFileTypes(array('pdf','doc'));

// Label for the upload button in the popup
$manager->setBrowseButtonText("Upload (PDF or DOC only)");

// In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
$manager->setGridLabelField('Name');

// Plural form of the objects being managed. Used on the "Add" button.
// If left out, this defaults to [MyObjectName]s
$manager->setPluralTitle('Resources');

$f->addFieldToTab("Root.Content.Resources", $manager);

return $f;
}

}

class ResourcePage_Controller extends Page_Controller
{
}
?>
----------------

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 April 2009 at 9:29am

What happens when you add $manager->setParentClass("Page") and $manager->setParentClass("ResourcePage"); to those functions, respectively?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 April 2009 at 9:30am

I believe this happens because there is no such thing as a PageID in most cases. Page has no unique fields so it has no table in the Database. I think SiteTree might be a more appropriate relationship.

I do know what you're talking about, though. I have used DataObjectMangaers to manage objects of a Page before and I have resolved the issue using setParentClass().

Avatar
Amir Mostofi

Community Member, 59 Posts

9 April 2009 at 9:37am

Adding $manager->setParentClass("Page") and $manager->setParentClass("ResourcePage") to respective functions and rebuilding the database unfortunately did not resolve anything. I had tried that before. PageID still remains 0 for ImageAttachments added to ResourcePage page(s).

What do you suggest? I'm at a loss here and need to finish the project quickly.

I really appreciate your help and patience.

Avatar
Amir Mostofi

Community Member, 59 Posts

9 April 2009 at 10:13am

UncleCheese, I think the only sure way of getting this to work is to not apply ImageAttachment directly to Page and then use it also in subclasses of Page (e.g. ResourcePage). Instead, I should remove it from Page class, create a subclass of Page, like PageWithImages and apply ImageAttachments to that and to ResourcePage class separately. Both of these classes are subclasses of Page and have no direct relationship to eachother. This works, but requires code duplication which is a bit of a shame!

Go to Top