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

Extended Dataobjects with a has_one relationship


Go to End


1289 Views

Avatar
WandL

Community Member, 9 Posts

3 December 2014 at 1:13pm

Edited: 03/12/2014 2:27pm

Hi Guys,

I am having problems understanding how to manage objects that relate to a has_one relationship when they are extended example:

Note I am using Grid Field Extensions (https://github.com/silverstripe-australia/silverstripe-gridfieldextensions) to allow the adding of the extended classes this way each time one is added I can have a different fields...

class Item extends DataObject {
	
	private static $has_one = array(
		'Type' => 'ItemType',
	);
	
	private static $belongs_to = array(
		'ItemTypes' => 'ItemType',
	);
	
	public function getCMSFields() {

		$fields = parent::getCMSFields();
		$gridConfig = GridFieldConfig_RelationEditor::create();
		$dataList = ItemType::get()->filter(array('ID'=>$this->Type()->ID));
		
		$gridField = GridField::create('TypeID', 'Type', $dataList, $gridConfig); 
		
		$gridField->getConfig()
		     ->removeComponentsByType('GridFieldAddNewButton')
		     ->removeComponentsByType('GridFieldAddExistingAutocompleter')
		     ->addComponent(new GridFieldAddNewMultiClass());
		
		$fields->addFieldToTab('Root.Type', $gridField);
	}
}

class ItemType extends DataObject {
	
	private static $has_many = array(
		'Items' => 'Item'
	);
}

class MyType extends ItemType {
	
	private static $db = array(
		'SomeField' => "Text",
	);
}

class AnotherType extends ItemType {
		
	private static $db = array(
		'AnotherField' => "Text",
	);
}

This all *seems* to work the problem is that it only allows one extended Type to be added if you add another of the same tClassName (ItemType) the same record gets updated in the extended table as its ID is the same as the ItemType->ID.

What am I missing here?

Any help greatly appreciated!