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

2.4 - DOM in SiteConfig not working for me


Go to End


39 Posts   13221 Views

Avatar
Carbon Crayon

Community Member, 598 Posts

18 April 2010 at 3:28am

Edited: 18/04/2010 3:43am

EDIT - Ok so I just tried CTF and it gave me this error on save:

Fatal error: Call to a member function getComponents() on a non-object in /home/aabfbo/public_html/dev/sapphire/forms/ComplexTableField.php on line 661

Which means this line is returning nothing:

$parentRecord = DataObject::get_by_id($data['ctf']['parentClass'], (int) $data['ctf']['sourceID']);

So I guess this is not a DOM issue, but perhaps you will be able to shed a little more light on it than I can? Looks like it's not getting the ID from the SiteConfig DataObject.... Thanks UC :)

/EDIT

Hi UC

I have been testing out using the DOM in the SiteConfig of 2.4 Rc1 using the DOM r393. here is my code:

DataObject:

<?php
class Link extends DataObject {

	static $has_one = array (
		'SiteConfig' => 'SiteConfig',
		'LinkedPage' => 'SiteTree'
	);

	static $summary_fields = array(
		'LinkedPage.Title' => 'Linked Page'
	);

	public function getCMSFields_forPopup()
	{
		$fields = new FieldSet();
		
		$fields->push(new SimpleTreeDropdownField('LinkedPageID', 'Link to Internal Page'));

		return $fields;
	}

}

CustomSiteConfig.php

<?php
 
class CustomSiteConfig extends Extension {
	
	function extraStatics() {
		return array(
			'has_many' => array(
				'Links' => 'Link'
			)
		);
	}
 
	public function updateCMSFields(FieldSet &$fields) {
		
		$manager = new DataObjectManager(
			$this->owner,
			'Links', 
			'Link', 
			Link::$summary_fields,
			'getCMSFields_forPopup'			
		);	
		$fields->addFieldToTab("Root.InfoMenu", $manager);
		
	}

}

It all appears to work fine until you add a dataobject. It adds the object to the database and it shows up in the DOM, however the relation ID on the DO (SiteConfigID) is not set and stays 0. So for some reason all the Link Objects show up in the table, yet none of them are actually attached to the SiteConfig so when try to use them in the templates nothing is returned.

If I am honest I am not totally sure if this should work, but it would be great if it could as having a DOM in siteconfig would be really great and I assume it's not all that different to having it in Model admin which seems to work fine.

Oh one other thing I noticed, the SimpleTreeDropdown does not seem to display properly in 2.4, instead of having an indentation, it displays like:


Page
&nbsp;&nbsp;SubPage

Cheers

Aram

www.SSBits.com - SilverStripe tutorials, tips and other bits

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 April 2010 at 4:27am

You probably need to use setParentClass() to make sure the foreign key gets set?

Avatar
Carbon Crayon

Community Member, 598 Posts

18 April 2010 at 4:49am

Hey UC, thanks for the (as usual) fast response.

Unfortunately it doesn't seem to make a difference. My DOM def now looks like this:

		$manager = new DataObjectManager(
			$this->owner,
			'Links', 
			'Link', 
			Link::$summary_fields,
			'getCMSFields_forPopup'
		);	
		$manager->setParentClass('SiteConfig');
		$fields->addFieldToTab("Root.Links", $manager);	

Avatar
Carbon Crayon

Community Member, 598 Posts

19 April 2010 at 10:27pm

Has anyone else got DOM or CTF working within the site config?

I am writing a post for SSBits about SiteConfig so would be nice to include DOM in that if it's possible....

Aram

www.SSBits.com - SilverStripe Tutorials, tips and other bits

Avatar
dhensby

Community Member, 253 Posts

20 April 2010 at 12:01am

Hey Aram,

I've had a similar problem before when trying to relate an item managed by DOM to another DO (rather than the page holding it). ParentID seems to get set as the holding page all the time.

So i had to extend the functionality a litte. (Uncy, might be worth adding to the core of DOM? It can be helpful to specifically define the ID of the parent you want).

class BBComplexTableField extends ComplexTableField {
	
	/**
	 * Set Source ID
	 *
	 * sets the source ID for the ComplexTableField
	 *
	 * @param int $val The ID of the source object
	 */
	function setSourceID($val) {
		if (is_numeric($val)) {
			$this->sourceID = $val;
		}
	}
	
	/**
	 * Source ID
	 *
	 * returns the sourceID as previously set or passed as a form field
	 *
	 * @return int the ID of the source object
	 */
	function sourceID() {
		if (isset($this->sourceID) && $this->sourceID !== null && is_numeric($this->sourceID)) {
			return $this->sourceID;
		}
		return parent::sourceID();
	}
}

Avatar
Carbon Crayon

Community Member, 598 Posts

20 April 2010 at 12:22am

Edited: 20/04/2010 1:09am

Great thanks Pigeon! That worked perfectly.

Tutorial can be found here:
http://ssbits.com/2-4-working-with-siteconfig/

Thanks again

Aram

www.SSBits.com - SilverStripe tutorials, tips and other bits

Avatar
Deklin

Community Member, 16 Posts

13 July 2010 at 1:35am

Edited: 13/07/2010 1:36am

Thank you both for this thread.

Aram, I found your tutorial useful and it works perfectly with the third-party data object manager however I cannot get it to work with the native ComplexTableField.

<?php

// Download this code with related files here: 
// http://sites.google.com/site/adsffadsf/Home/CustomSiteConfig.zip

class CustomSiteConfig extends Extension {

    function extraStatics() {
        return array('has_many' => array('Links' => 'Link'));
    }

    public function updateCMSFields(FieldSet & $fields) {
        $fields = parent::getCMSFields();
        $tablefield = new HasManyComplexTableField($this->owner, 'Links', 'Link', array('Link' => 'Link', 'Text' => 'Link Text',), 'getCMSFields_forPopup');
        $tablefield->setSourceID($this->owner->ID);
        $fields->addFieldToTab("Root.FooterLinks", $tablefield);
    }

    function setSourceID($val) {
        if (is_numeric($val)) {
            $this->sourceID = $val;
        }
    }

    function sourceID() {
        if (isset($this->sourceID) && $this->sourceID !== null && is_numeric($this->sourceID)) {
            return $this->sourceID;
        }
        return parent::sourceID();
    }
}

?>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

13 July 2010 at 1:44am

It's a little odd that your sourceID() functions are in CustomSiteConfig, but you're calling them on a CTF object. ???

Go to Top