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

Gridfield with relationEditor has_one Dataobject


Go to End


6 Posts   3301 Views

Avatar
Matulko

Community Member, 9 Posts

29 June 2012 at 8:05pm

hello
I got few guestions about gridfield and creating new dataobject there

I want create photogallery for article pages so I got this code

class ArticlePhoto extends DataObject
{
	public static $singular_name = 'Obrázok k článku';
	
	public static $db = array (
		'Title' => 'text'
	);
	
	public static $has_one = array (
		'Image' => 'Image',
		'Page' => 'Page',
		);
}

class Article extends Page
{

	public static $has_many = array(
		'ArticlePhotos' => 'ArticlePhoto'
	);
	
	public function getCMSFields()
	{
		$f = parent::getCMSFields();
		
		$dl = DataList::create('ArticlePhoto')
			->where('PageID ='.$this->ID);
		
		$config = new GridFieldConfig_RelationEditor();
				
		$gridField = new GridField('ArticlePhotos','ArticlePhotoGallert', $dl,$config);
				
		$f -> addFieldToTab('Root.PhotoGallery', $gridField);
		return $f;
	}
}

I want use this DataObject for more page subclasses so for that is is in has_one Page class

it looks like really simple code but my probles is how to set automatically dataobject has_one page

when I create new dataobject this is not automatically set and I am redirected to page with ID = 1
when I delete filter '->where('PageID ='.$this->ID);' everything works fine but I still have to set this relation

I want just create DataObject and then upload Image without set this relation
any suggestions???

Avatar
elgordo

Community Member, 70 Posts

10 February 2013 at 12:45am

Noticed a typo in your code, ArticlePhotoGallert - should this be ArticlePhotoGallery?

Avatar
elgordo

Community Member, 70 Posts

10 February 2013 at 12:45am

Noticed a typo in your code, ArticlePhotoGallert - should this be ArticlePhotoGallery?

Avatar
SamTheJarvis

Community Member, 24 Posts

26 February 2013 at 4:30am

Edited: 26/02/2013 4:34am

This is a problem I have too. There doesn't seem to be any solution for adding/editing/deleting objects that are attached through a has_one.

Sorry I can't help, I'm looking into a way to do this.

Avatar
Matulko

Community Member, 9 Posts

26 February 2013 at 8:43pm

Edited: 26/02/2013 8:44pm

thanks for help
i started using multiple upload field for this

Avatar
patricknelson

Community Member, 7 Posts

17 August 2016 at 11:00am

For anyone still interested in implementing the ability to edit a `has_one` to another `DataObject` inline, they might find this particular extension helpful:

https://github.com/stevie-mayhew/hasoneedit