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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

$has_one with SiteTree links to page and doesn't show SimpleTreeDropDown


Go to End


3 Posts   1496 Views

Avatar
updog

Community Member, 13 Posts

10 January 2011 at 2:16am

I am building a service comparison chart that sits on a ServiceComparisonPage type. The data objects aren't necessarily linked to the page type but as there will be only one of these charts in the site, it will make it easier for the user to edit it here.

I have managed to use the DataObjectManager to build a new tab for each dataobject and allow management of objects. The problem I am having is that I want each service object to be able to link to any page in the site however because the management of data objects is on a Page class, the LinkToID field is hidden on the pop up form and set to the page that it is contained in by default. Code below FYI.

// ServiceComparisonPage

<?php
class ServiceComparisonPage extends Page {

public static $db = array(
);

public static $has_one = array(
);

function getCMSFields() {

$fields = parent::getCMSFields();

$fields->addFieldToTab("Root.Content.Services", new DataObjectManager(
$this,
'Services',
'Service',
null,
'getCMSFields_forPopup'
));

return $fields;
}

}
class ServiceComparisonPage_Controller extends Page_Controller {

public static $allowed_actions = array (
);

public function init() {
parent::init();

}

public function Services()
{
return DataObject::get('Service');
}

public function FeatureGroups() {
return DataObject::get('FeatureGroup');
}

}

// This is the Service Object

class Service extends DataObject {

static $db = array(
'Title' => 'Text'
);

static $many_many = array(
'Features' => 'Feature',
);

static $has_one = array(
'LinkTo' => 'SiteTree'
);

public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Title'),
new SimpleTreeDropdownField(
'LinkToID',
'Please select the page that this service links to...'
)
);
}

}

Avatar
dhensby

Community Member, 253 Posts

10 January 2011 at 3:51am

This problem is caused because ComplexTableField (that DOM enhances) tries to be 'clever' and automatically guesses that the has_one SiteTree relationship is meant to link between the object and the page you're managing the object on.

From what you've said, the objects arent linked to specific page instances, so you want to turn this feature off. So you'll need to do something like this:

...
$dom = new DataObjectManager(
         $this, 
         'Services', 
         'Service', 
         null, 
         'getCMSFields_forPopup' 
);
$dom->setRelationAutoSetting(false);
$fields->addFieldToTab('Root.Content.Services',$dom);
...

Hope that sorts this out for you.

Avatar
updog

Community Member, 13 Posts

10 January 2011 at 1:03pm

Thanks. Solved!