Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » ImageDataObjectManager showing all photos on every page

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: 1596 Views
  • socks
    Avatar
    Community Member
    190 Posts

    ImageDataObjectManager showing all photos on every page Link to this post

    I have an odd gallery implementation, so I've decided to use the ImageDataObjectManager.

    I've added it to Page.php so I could have a separate gallery for each page. It seems to be working when I look at the CMS (it's only showing the images I've uploaded for that specific page). But when viewing the site, every page is outputting all photos that were added no matter what page they were uploaded to.

    Thanks

    GalleryPhoto.php

    class GalleryPhoto extends DataObject {
       
       static $db = array (
          'Category' => 'Varchar',
          'PhotoCredit' => 'Varchar(200)'
       );

       static $has_one = array (
          'Attachment' => 'File',
          'Page' => 'Page'
       );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Category'),
             new TextField('PhotoCredit'),
             new FileIFrameField('Attachment')
          );
       }

    }

    Page.php

    static $has_many = array (
       'PhotoGallery' => 'GalleryPhoto'
    );
    ...
    function Lightbox() {      
       return DataObject::get('GalleryPhoto');
    }

    Page.ss

    <% control Lightbox %>
       $Attachment
    <% end_control %>

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: ImageDataObjectManager showing all photos on every page Link to this post

    The problem is your Lightbox getter method.
    Since you're doing return DataObject::get('GalleryPhoto'); without any where clause you will get all the gallery photos back.
    If all you want is to get the Gallery photos related to the current page then you don't need to write your own method.

    Just write this in your template instead.

    <% control PhotoGallery %>
    $Attachment
    <% end_control %>

  • UncleCheese
    Avatar
    4085 Posts

    Re: ImageDataObjectManager showing all photos on every page Link to this post

    If you're going to use a DOM to manage relationships on subclasses, you need to set its parent class. Think about it -- you're passing $this as the controller, so on the class PageSubclass, the DOM will try to set the PageSubclassID for that object, when in fact, it should be a PageID.

    $myImageDOM->setParentClass("Page");

  • socks
    Avatar
    Community Member
    190 Posts

    Re: ImageDataObjectManager showing all photos on every page Link to this post

    I first tried using the source name (PhotoGallery) instead of the source class, and that seemed to do the trick.

    Thank you both!

    1596 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.