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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

3.1 DataObject with multiple image-upload


Go to End


2 Posts   1497 Views

Avatar
timo

Community Member, 47 Posts

5 October 2014 at 9:32pm

Edited: 05/10/2014 9:33pm

Hi!
I am struggling to get a DataObject working with multiple image-upload.
Its working with a has_one relation as the code shows.
I can't find out the correct relations when trying to set this up with a has_many images relation. ( in MyImage )
Thanks! timo.

ProjectPage:

class ProjectPage extends Page {
	
	static $has_many = array(
		'ProjectItems' => 'ProjectItem'
	);
	
	static $description = "ProjektSeite";
	static $singular_name = 'Projekt';
	
	public function getCMSFields()
	{
		$fields = parent::getCMSFields();
		$fields->removeFieldFromTab("Root.Main","Content");
		
		$GridFieldConfig = GridFieldConfig_RecordEditor::create();
		
		$GridFieldConfig->removeComponentsByType('GridFieldPaginator');
		$GridFieldConfig->addComponent(new GridFieldPaginator(20));
		$GridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
		
		$Gridfield = new GridField("ProjectItems", "Projekte", $this->ProjectItems()->sort("SortOrder"), $GridFieldConfig); 
		$fields->addFieldToTab('Root.'.$this->Title, $Gridfield);
		
		return $fields;
	}

ProjectItem:

class ProjectItem extends DataObject
{
	static $db = array (
		'SortOrder' => 'Int',
		'Title' => 'Varchar(255)',
		'ShortDescription' => 'Text',
		'Description' => 'Text',
		'ExternalLink' => 'Varchar(255)',
		'ServiceText' => 'Text'
	);
	
	static $has_one = array (
		'ProjectPage' => 'ProjectPage',
		'Thumbnail' => 'Image'//,
		//'Images' => 'MyImage'
	);
	
	static $has_many = array (
		'Images' => 'MyImage'
	);
	
	/*public static $many_many = array(
		'Images' => 'MyImage'
	);*/
	
	static $summary_fields = array(
		'Thumb' => 'Thumbnail',  
		'Title' => 'Title'
	);
	
	static $singular_name = 'Projekt';
	static $plural_name = 'Projekte';
	
	public function getCMSFields()
	{
		
		$fields = parent::getCMSFields();
		
		
		$fields->removeFieldFromTab("Root.Main","SortOrder");
		$fields->addFieldToTab("Root.Main", new TextField('Title', 'ProjektName'));
		$fields->addFieldToTab("Root.Main", new TextareaField('ShortDescription', 'KurzText'));
		$fields->addFieldToTab("Root.Main", new TextareaField('Description', 'Text'));
		$fields->addFieldToTab("Root.Main", new TextField('ExternalLink', 'Link'));
		$fields->addFieldToTab("Root.Main", new TextareaField('ServiceText', 'Leistungen'));
		$fields->addFieldToTab("Root.Main", new UploadField('Thumbnail', 'Thumbnail'));
		
		
		$myImageGridFieldConfig = GridFieldConfig_RecordEditor::create(); 
		$myImageGridFieldConfig->addComponent(new GridFieldBulkManager());
		$myImageGridFieldConfig->removeComponentsByType('GridFieldPaginator');
		$myImageGridFieldConfig->addComponent(new GridFieldPaginator(20));
		$myImageGridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
		
		$fields->addFieldToTab("Root.Images", new GridField("Images", "Bilder", $this->Images()->sort("SortOrder"), $myImageGridFieldConfig)); 
		
		
		
		
		return $fields;
	}

MyImage:

class MyImage extends DataObject {
	
	static $db = array (
		'SortOrder' => 'Int'
	);
	
	static $has_one = array ( 
		'Image' => 'Image',
		'ProjectItem' => 'ProjectItem' 
	);
	
	/*static $has_many = array ( 
		'Images' => 'Image'
	);*/
		
	/*public static $belongs_many_many = array(
		'ProjectItem' => 'ProjectItem'
	);*/	
	
	static $singular_name = 'Bild';
	static $plural_name = 'Bilder';
	
	public function getCMSFields(){ 
  		$fields = parent::getCMSFields();

		$fields = new FieldList(
			new UploadField('Image','Photo')
		);
		
		return $fields;
	}

Avatar
martimiz

Forum Moderator, 1391 Posts

9 October 2014 at 3:56am

Edited: 09/10/2014 3:57am

I'm not sure what you are actually trying to accomplish, and what the problem is. But I know it's best not to use a has_many relation to an Image object - use a many_many relation instead. I've had it working nicely together with the SortableFile module:

http://addons.silverstripe.org/add-ons/bummzack/sortablefile