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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Assigning ImageDataObjectManager to a DataObject


Reply


7 Posts   915 Views

Avatar
Lazarus404

Community Member, 71 Posts

22 October 2010 at 3:03am

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

Avatar
UncleCheese

4086 Posts

22 October 2010 at 3:10am

Edited: 22/10/2010 3:11am

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

Avatar
Lazarus404

Community Member, 71 Posts

22 October 2010 at 4:21am

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

Avatar
Lazarus404

Community Member, 71 Posts

22 October 2010 at 4:51am

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

Avatar
Lazarus404

Community Member, 71 Posts

22 October 2010 at 6:21am

So, I've found a post from a guy doing the same thing as [url=http://www.silverstripe.org/dataobjectmanager-module-forum/show/285450]I am[/url], except it's working for this guy (and not for me). Looking at the code from the [url=http://silverstripe.org/dataobjectmanager-module-forum/show/274900#post274900]post he linked to his[/url], 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... :(

Avatar
Lazarus404

Community Member, 71 Posts

22 October 2010 at 6:34am

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

Avatar
UncleCheese

4086 Posts

22 October 2010 at 6:38am

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