Jump to:

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
Author Topic: 54221 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: Preview: DataObjectManager module Link to this post

    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.

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    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
    {
    }
    ?>
    ----------------

  • UncleCheese
    Avatar
    4085 Posts

    Re: Preview: DataObjectManager module Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Preview: DataObjectManager module Link to this post

    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().

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    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.

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    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!

  • Mr. Matt
    Avatar
    Community Member
    5 Posts

    Re: Preview: DataObjectManager module Link to this post

    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.

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    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

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.