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

Preview: DataObjectManager module


Go to End
Reply


379 Posts   63308 Views

Avatar
UncleCheese

Forum Moderator, 4096 Posts

8 April 2009 at 2:04pm

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?

Avatar
Amir Mostofi

Community Member, 59 Posts

8 April 2009 at 2:10pm

Edited: 08/04/2009 2:11pm

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!

Avatar
Amir Mostofi

Community Member, 59 Posts

8 April 2009 at 5:09pm

Edited: 08/04/2009 5:11pm

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?

Avatar
Mr. Matt

Community Member, 5 Posts

8 April 2009 at 5:40pm

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.

Avatar
Amir Mostofi

Community Member, 59 Posts

8 April 2009 at 5:54pm

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?

Avatar
drye

Community Member, 49 Posts

8 April 2009 at 11:35pm

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.

Avatar
Amir Mostofi

Community Member, 59 Posts

8 April 2009 at 11:55pm

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.

Avatar
Amir Mostofi

Community Member, 59 Posts

8 April 2009 at 11:58pm

Edited: 09/04/2009 12:01am

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?

Go to Top