Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Preview: DataObjectManager module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Go to End
Author Topic: 60044 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: Preview: DataObjectManager module Link to this post

    First of all, this is really eerie. I literally created exactly the same code using the FileDataObjectManager about a week ago. Exactly the same class names and relationship names. What a weird coincidence that is. So similar, I was actually starting to worry that someone had hacked into my server and stolen the code.

    Anyway..

    The most flagrant error I see in your code is that your template is called PartnerLogo.ss, but your Page class is PartnerPage. What's up with that?

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    Thank you UncleCheese! The issue has been sorted. PartnerLogo.ss is just an Include file. The reason <% if Partners %> was not returning anything was because I hadn't added a check to see if I was actually on the Partner page. All sorted now.

    BTW, this is by far the most useful SS module! Many Thanks!

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    I'm trying to associate multiple images with the basic page type "Page" using FileDataObjectManager. Below are my files:

    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(
          'HeaderImage' => 'Image'
       );
       
       static $has_many = array (
          'ImageAttachments' => 'ImageAttachment'
       );

       public function getCMSFields()
       {
          $f = parent::getCMSFields();

          $f->addFieldToTab("Root.Content.HeaderImage", new ImageField('HeaderImage'));

          $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
          );
          
          $manager->setParentClass('Page');
          
          // 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 {
    ...   
    }

    ?>

    -------------

    The filter clause "PageID = {$this->ID}" was added to ensure that only the ImageAttachments that have PageID set to the current page ID are actually displayed in the list, instead of all images which have been added.

    Now, this works. However, when you actually attempt to add an attachment, PageID is not set to the current page ID. It's left as '0'. Only when I change the PageID directly in the database (e.g. using phpMyAdmin) can I actually see the filter "PageID = {$this->ID}" working.

    Incidentally, line $manager->setParentClass('Page'); does not seem to make any difference.

    In short, how do you go about using FileDataObjectManager to associate multiple images with page type "Page" while showing only those images with PageID set to the current page?

  • Mr. Matt
    Avatar
    Community Member
    5 Posts

    Re: Preview: DataObjectManager module Link to this post

    amir.mostofi your not the only one getting that problem also, so are we, i've spent the afternoon trying to work out what is going on, and from what I have seen the problem lies within either FileDataObjectManager or ImageDataObjectManager.

    We were originally using a complex table field to manage multiple images on one page using the setParentClass( 'Page' ); and it was all working fine. I changed the complex table field to the ImageDataObjectManager and thats when it stopped working. We know the imagedataobjectmanager is setup ok, but as others have found the PageID hasn't been set correctly in the hidden input field auto generated in the html. So in my getCMSFields_forPopup function that the ImageDataObjectManager was calling, I did a print_r( $this ) and that shows the PageID is being set in there fine.

    So instead of using the ImageDataObjectManager we tried using DataObjectManager to see if it was still happening and have found that it all works fine there, the PageID is being set fine.

    So I reckon the problem lies in either the ImageDataObjectManager or the FileDataObjectManager (Seeing as ImageDataObjectManager extends from FileDataObjectManager which extends off DataObjectManager) and somewhere the PageID is being overwritten, so that is where I am focusing my search.

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    Mr. Matt, thank you for your input. It's great to know that the issue is a common one!

    I actually don't mind using the DataObjectManager, as I don't necessarily need all of the functionalities of FileDataObjectManager. If I were to use DataObjectManager, can you tell me how I can make sure that the images displayed in a list are set to a fixed width (say 100 px) as opposed to being displayed in their original size?

    For example, consider the following 2 files:

    Testimonial.php
    -----------
    <?php
    class Testimonial extends DataObject
    {
       static $db = array (
          'Date' => 'Date',
          'Author' => 'Text',
          'Quote' => 'Text'
       );
       
       static $has_one = array (
          'TestimonialPage' => 'TestimonialPage',
          'Image' => 'Image'
       );
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new CalendarDateField('Date'),
             new TextField('Author'),
             new SimpleHTMLEditorField('Quote','',array('justifyFull' => true)),
             new ImageField('Image')
          );
       }

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

    TestimonialPage.php
    --------------
    <?php
    class TestimonialPage extends Page
    {
       static $has_many = array (
          'Testimonials' => 'Testimonial'
       );
       
       public function getCMSFields()
       {
          $f = parent::getCMSFields();
          $manager = new DataObjectManager(
             $this, // Controller
             'Testimonials', // Source name
             'Testimonial', // Source class
             array('Date' => 'Date', 'Author' => 'Author', 'Quote' => 'Quote', 'Image' => 'Image'), // Headings
             'getCMSFields_forPopup' // Detail fields function or FieldSet
             // Filter clause
             // Sort clause
             // Join clause
          );
          
          
          $f->addFieldToTab("Root.Content.Testimonials", $manager);
          
          return $f;
       }

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

    As defined by array('Date' => 'Date', 'Author' => 'Author', 'Quote' => 'Quote', 'Image' => 'Image'), the image in the list is displayed at full size. How can I use setWidth(100) or resizeByWidth(100) in this case?

  • drye
    Avatar
    Community Member
    49 Posts

    Re: Preview: DataObjectManager module Link to this post

    Mr. Matt and amir.mostofi,

    Please read the docs at: http://doc.silverstripe.org/doku.php?id=modules:dataobjectmanager

    What I have pasted here should work, except I am not sure about swf, so I took that functionality away:
    http://pastie.org/440610

    Good luck.

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    Drye,

    Thank you for your help.
    I have read the docs many times. Unfortunately, your code does not resolve the issue. Using your code, all the files added are always displayed within the Images list on all Pages which have had images added to them, regardless of their PageID.
    On any one page, we want to display only the images added to that page (i.e. which have PageID equal to the ID of that page), not all the images added.

    This issue seems to occur when dealing directly with the main Page class, whether you use FileDataObjectManager or ImageDataObjectManager. It does not, however, appear if you use another class which extends Page.

  • Amir Mostofi
    Avatar
    Community Member
    59 Posts

    Re: Preview: DataObjectManager module Link to this post

    Drye,

    Would you know how to use the basic DataObjectManager to add files/images and have the images displayed at a fixed width of say 100 px within the list rather than as their original size?

    60044 Views
Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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.