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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Multiple images attached to each page


Go to End


3 Posts   7296 Views

Avatar
Lime Blast

Community Member, 22 Posts

23 May 2013 at 12:22am

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 :)

Avatar
Lime Blast

Community Member, 22 Posts

23 May 2013 at 12:41am

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 :)

Avatar
MJA

Community Member, 21 Posts

24 May 2013 at 4:46am

Edited: 24/05/2013 4:56am

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