Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Assigning ImageDataObjectManager to a DataObject

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 883 Views
  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Assigning ImageDataObjectManager to a DataObject Link to this post

    Hey guys,

    I'm having trouble using the ImageDataObjectManager in my app. It works great if I assign an ImageDataObjectManager directly to a pages getCMSFields method, but in my particular case, a page can have assigned to it multiple Projects (using ComplexTableField) and each Project can have multiple images (thus the ImageDataObjectManager). When I put the setup for the ImageDataObjectManager in the Project classes getCMSFields_forPopup, it fails. I see a tab created for it in the popup, but the tab contains a single line table with no means to add / edit / remove items.

    Here's the code:

    Page

    class GalleryPage extends Page
    {
       static $singular_name = 'Gallery page';
       static $plural_name = 'Gallery pages';
       
       static $has_many = array (
          'Projects' => 'Project'
       );

       function getCMSFields()
       {
          $fields = parent::getCMSFields();
          $projectsTable = new ComplexTableField($this, 'Projects', 'Project');
          $fields->addFieldToTab('Root.Content.Projects', $projectsTable);
          return $fields;
       }
    }

    Project

    class Project extends DataObject
    {
       static $db = array (
          'Title' => 'Varchar(255)',
          'Description' => 'Text',
          'Technologies' => 'Varchar(255)'
       );

       static $has_many = array (
          'GalleryImages' => 'GalleryImage'
       );
       
       static $has_one = array (
          'BelongToGalleryPage' => 'GalleryPage'
       );
       
       function getCMSFields_forPopup()
       {
          $manager = new ImageDataObjectManager(
             $this, // Controller
             'GalleryImages', // Source name
             'GalleryImage', // Source class
             'GalleryImageURL', // File name on DataObject
             array(
                'Label' => 'Title'
             ),
             'getCMSFields_forPopup'
          );
          
          $manager->setSingleTitle('Gallery');
          $manager->setPluralTitle('Gallery');
          
          $fields = new FieldSet(
             new TextField('Title'),
             new SimpleHTMLEditorField('Description'),
             new TextField('Technologies'),
             $manager
          );
          return $fields;
       }
    }

    GalleryImage

    class GalleryImage extends DataObject
    {
       static $db = array (
          'Label' => 'Text'
       );
       
       static $has_one = array (
          'GalleryImageURL' => 'Image', //relation needed for this DataObject
          'BelongToProject' => 'Project' //relation needed to point back to your pagetype, my pagetype is Project.
       );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Label'),
             new FileIFrameField('GalleryImageURL')
          );
       }
       
       public function IsFirst(){
        return $this->iteratorPos == 0;
       }
    }

    The code looks good to me (logically), and being able to get it running directly in the page shows I have all the necessary module stuff installed. Can anyone see what I might be doing wrong?

    Thanks loads,
    Lee

  • UncleCheese
    Avatar
    4085 Posts

    Re: Assigning ImageDataObjectManager to a DataObject Link to this post

    Well the biggest problem I see is that you're mixing ComplexTableField and DataObjectManager.. ??? The CTF on your GalleryPage needs to be a DOM.

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: Assigning ImageDataObjectManager to a DataObject Link to this post

    Thank you. That's gotten me closer. The gallery still doesn't have all the controls that it should, but it's not totally broken, now, either. I'll have a fiddle with this and get back to you if I'm still struggling ;) I've only started using SilverStripe for the first time, today, so there's a lot I'm yet to discover.

    Thanks again,
    Lee

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: Assigning ImageDataObjectManager to a DataObject Link to this post

    Okay, so I changed the CTF to a DOM type, which works 'better'. Now, when I click on the popups Gallery Images tab, I can click an Add Image button and upload an image. Once the image popup has closed, though, the data doesn't appear. If I click the #1 link on the image upload popup, I can see the controls for uploading an image, but uploading clears it. It's almost as if the data isn't being stored to the database.

    I've not made any permanent changes beyond the CTF -> DOM change. Any ideas?

    Thanks,
    Lee

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: Assigning ImageDataObjectManager to a DataObject Link to this post

    So, I've found a post from a guy doing the same thing as I am, except it's working for this guy (and not for me). Looking at the code from the post he linked to his, I can't see any differences, aside from the fact that he must have moved his image reference from $has_one to $has_many.... It doesn't seem possible that mine should be failing...

  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Re: Assigning ImageDataObjectManager to a DataObject Link to this post

    Well, it seemed I was right. My code was near perfect. However, near isn't good enough. I'd left the getCMSFields_forPopup method in Project from when I was testing ImageDataObjectManager in the page. Having moved it into Project, I should have lopped off the _forPopup part. I've made that change, now, and all is working beautifully.

    Thanks for the help, UC. That CTF issue would have certainly left me crying myself to sleep, tonight ;-)

    Lee

  • UncleCheese
    Avatar
    4085 Posts

    Re: Assigning ImageDataObjectManager to a DataObject Link to this post

    Ahh, there ya go. Yeah, I've tried to deprecate that "_forPopup" stuff. It's rare that you'd want different fields in your popup than anywhere else, so it's best to use the standard "getCMSFields"..

    Glad you go it working!

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

    883 Views
Page: 1
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.