7911 Posts in 1354 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Preview: DataObjectManager module
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 54221 Views |
-
Re: Preview: DataObjectManager module

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.
-
Re: Preview: DataObjectManager module

9 April 2009 at 9:17am Last edited: 9 April 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
----------------
<?phpclass 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
{
}
?>
---------------- -
Re: Preview: DataObjectManager module

9 April 2009 at 9:29am
What happens when you add $manager->setParentClass("Page") and $manager->setParentClass("ResourcePage"); to those functions, respectively?
-
Re: Preview: DataObjectManager module

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().
-
Re: Preview: DataObjectManager module

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.
-
Re: Preview: DataObjectManager module

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!
-
Re: Preview: DataObjectManager module

9 April 2009 at 10:16am
Setting $var->setParentClass( 'Page' ); to the DataObjectManager fixes the issue, its when you try and use ImageDataObjectManager and FileDataObjectManager that it doesnt fix the problem any more.
Something is happening when the form is built that PageID is not being listened to in the FileDataObjectManager upon creation of new items.
-
Re: Preview: DataObjectManager module

9 April 2009 at 10:37am
I have a basic question regarding the front-end templating and checking the extension of a file attachment to perform a certain function.
Imagine that we have a database file field $Attachment. $Attachment.Extension will return the extension. Now, assuming that $Attachment has both SWF and JPG files, I want to detect on the front-end template side that if extension is SWF, write the HTML code for displaying the flash file and if not, display $Attachment which will return the img tag.
I know that something like <% if $Attachment.Extension = SWF %>....<% else %>....<% end_if %> will naturally give parsing error. Then, how do you go about this?
| 54221 Views | ||
| Go to Top | Next > |

