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, Ed, biapar, Willr, Ingo, swaiba

Image upload from with Thumbnail Image.

Go to End

2 Posts   1410 Views


Community Member, 8 Posts

28 May 2011 at 2:20pm


I'm trying to build a form to allow users to attach a file to page. This more of less works but I can't get it to create the thumbnail image afterwards.

Can someone please tell why?


class ArticlePage extends Page {
	static $icon = "themes/tutorial/images/treeicons/news";
	static $defaults = array(
	'ProvideComments' => true

	static $db = array(
		'Date' => 'Date',
		'Author' => 'Text'
	static $has_one = array(
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date','Article Date (for example: 20/12/2010)'), 'Content');
		$dateField->setConfig('showcalendar', true);
		$dateField->setConfig('dateformat', 'dd/MM/YYYY');
		$fields->addFieldToTab('Root.Content.Main', new TextField('Author','Author Name'), 'Content');
		return $fields;
class ArticlePage_Controller extends Page_Controller {

	public function ApplyForm() {
		$form = new Form (
			new FieldSet (
				new TextField('ViewTitle', 'Title'),
				new TextareaField('ViewDescription', 'Description'),
				new HiddenField('ArticleTitle', _t('FileAttach.ARTICLETITLE'), $this->Title ),
				new HiddenField('ArticleID', _t('FileAttach.ARTICLEID'), $this->ID ),
				$file = new FileUploadField('Attachment', _t('FileAttach.ATTACHMENT','Upload a File'))
			new FieldSet (
			new FormAction('doApply', _t('FileAttach.ATTACHMENT','Add File'))
			new RequiredFields('ViewTitle')
		return $form;

	public function doApply($data, $form) {
		$form->saveInto($application = new FileAttach());
		return array (
		'Application' => $application

	function GetFiles() {
		$id = $this->ID;
		$obj			= "FileAttach";
		$filter 		= "ArticleID = $id";
		$sort			= "";
		$join			= "LEFT JOIN File ON (File.ID = FileAttach.AttachmentID)";
		$limit			= "";
		$data =  DataObject::get($obj, $filter, $sort, $join, $limit);
		return $data;


<% include Menu2 %>
<div id="Content" class="typography">
    <% include Breadcrumbs %>

    <div class="newsDetails">
        PAGE ID $ID Posted on $Date.Nice by $Author
<% control GetFiles %>
		<div>$ID $ViewTitle $ViewDescription</div>
<% end_control %>


class FileAttach extends DataObject {

	static $db = array (
		'ViewTitle' => 'Text',
		'ViewDescription' => 'Text',
		'ArticleTitle' => 'Text',
		'ArticleID' => 'Varchar',

	static $has_one = array (
		'Attachment' => 'File'

	public function getCMSFields_forPopup() {
	      return new FieldSet(
	         new TextField('ViewTitle', 'Title'),
	         new TextareaField('ViewDescription', 'Description'),
	         new ImageField('File', 'File')

	public function getCMSFields() {
		$f = parent::getCMSFields();
		$manager = new ImageDataObjectManager(
		$this, // Controller
		'Attachment', // Source name
		'File', // Source class
		'ApplyForm', // File name on DataObject
		'Title' => 'Title', 
		'Caption' => 'Caption'
		), // Headings 
		'getCMSFields_forPopup' // Detail fields
		// Filter clause
		// Sort clause
		// Join clause
 		return $f;


Forum Moderator, 4102 Posts

31 May 2011 at 3:14pm

Edited: 31/05/2011 3:14pm

The reason you're not getting thumbnails is because you've casted the file relation as "File" rather than "Image", so you're not getting any of the functions contained in the Image class.

But there are some bigger problems going on with your datamodel. Your GetFiles() function is needlessly complex. Get rid of that all together.

Add a has_many to your ArticlePage:

static $has_many = array (
'FileAttachments' => 'FileAttach'

And in your FileAttach class, you've put a foreign key in your DB array. That's no good. Use the ORM!!


$has_one = array (
'Attachment' => 'Image',
'Article' => 'ArticlePage'

Then on your template, you just need to do:

<% control FileAttachments %>

<% end_control %>

Life is so much easier when you use the tools SS gives you! :-)

Silverstripe tips, tutorials, screencasts, and more.