Jump to:

3428 Posts in 1056 Topics by 733 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Multiple images attached to each page

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

Page: 1
Go to End
Author Topic: 1309 Views
  • Lime Blast
    Avatar
    Community Member
    22 Posts

    Multiple images attached to each page Link to this post

    I'm working on a project where each page on the site needs to have anywhere from one to many images attached to it. These are not images which exist within the page's content, but rather separate images which I'll need to manipulate into things like thumbnails, etc..

    Having followed the tutorials, along with a little bit of guess work, I've managed, using the following code, to get something working:

    /mysite/code/Page.php

    class Page extends SiteTree
    {

       public static $db = array();

       public static $has_one = array();

       public static $has_many = array(
          'Photos' => 'Photo'
       );

       public function getCMSFields()
       {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Images', new UploadField('Photos'));
          return $fields;
       }

    }

    /**
    * Allows the upload of multiple images to pages
    * http://www.silverstripe.org/general-questions/show/20345#post315482
    */
    class Photo extends Image
    {
       public static $has_one = array(
          'Page' => 'Page'
       );
    }

    As this is the base Page class, which all my other extend, it means with very little effort I've been able to add image uploading to every page on the site, accessing them in the theme files like so (for example)..

    <% if $Photos %>
       <% loop $Photos %>
          $CroppedImage(630,320)
       <% end_loop %>
    <% end_if %>

    This is fine, so far, but it has a couple of problems with which I'm looking for some help.

    The first issue is that I'm only able to have each image attached to one page, where as I was hoping for a more flexible system which would let me attach the same image to multiple pages. I realise that I could just upload multiple copies of the same image, but that isn't really fixing the problem, it's just bypassing it.

    Second, I'm looking for a way in which I'm able to order the images, so that when I do a loop (like above), the image I assigned as first in the CMS is the first in the loop.

    Can anyone help point me in the right direction on how I'm able to fix these issues?

    Thank you

  • Lime Blast
    Avatar
    Community Member
    22 Posts

    Re: Multiple images attached to each page Link to this post

    OK, quick update - I think I've solved my first problem. Having read some info here: http://www.silverstriperesources.com/articles/silverstripe-3-many-many-a-comprehensive-example/ - I've changed my code thusly:

    class Page extends SiteTree
    {

       public static $db = array();

       public static $has_one = array();

    //   public static $has_many = array(
    //      'Photos' => 'Photo'
    //   );

       public static $many_many = array(
          'Photos' => 'Photo'
       );

       public function getCMSFields()
       {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Images', new UploadField('Photos'));
          return $fields;
       }

    }

    /**
    * Allows the upload of multiple images to pages
    * http://www.silverstripe.org/general-questions/show/20345#post315482
    */
    class Photo extends Image
    {
    //   public static $has_one = array(
    //      'Page' => 'Page'
    //   );

       public static $many_many = array(
          'Page' => 'Page'
       );
    }

    So far as I can tell, this appears to be working as intended and has fixed the issue of having the same image attached to multiple pages (although if someone could just check over my code to make sure I've not done anything silly).

    I've still got the second issue of ordering the images however, can someone help? Cheers

  • MJA
    Avatar
    Community Member
    15 Posts

    Re: Multiple images attached to each page Link to this post

    OK Lime Blast, you're definitely on the right track here

    I read your first post and immediately though -- xref file (that's the old database programmer in me speaking)

    Your second post (putting a many-many relationship between Page and Photo) does just that

    If you look at your database (using a tool like phpMyAdmin) you should find a table called Page_Photo (or something like that) containing just two fields:
       PhotoID => foriegn key to Photo
       PageID   => foriegn key to Page

    To get the image ordering sorted out you could try creating a DataObject extention for this table and adding a sort field
    Something like:

       class Page_Photo extends DataObject {
          public static $has_one = array (
             'Page'   => 'Page',
             'Photo'   => 'Photo'
          );
          public $db = array (
             'SortOrder'   => 'Int'
          );
       }

    You should then be able define the image order in the back end page definition (you may have to do a little work with getCMSFields)

    You will then need to add a select function in the Page_Controller class to return the image records in order of SortOrder (you may need to read the xref table and join to the image table) and call this in your template.

    I must admit I have not tried this so I don't know if it will actually work, but it does seem logically correct so it's probably worth a go

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