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

Extend SiteTree using DataObjectDecorator


Go to End


5 Posts   3403 Views

Avatar
chrclaus

Community Member, 29 Posts

17 July 2009 at 7:34am

Hello,

I extended the SiteTree (Silverstripe v2.3.2) with a DataObjectDecorator and want to access the newly added attribute in the decorator:

class SiteTreeCategory extends DataObjectDecorator {
...
	function extraDBFields() {
		return array(
			'has_one' => array('Category' => 'Category')
		);}
	
	function updateCMSFields(FieldSet &$fields) {
	    $category = $this->owner->Category();
...
	}	
}

The decorator is registered in _config.php

DataObject::add_extension('SiteTree', 'SiteTreeCategory');

While accessing the admin section I receive the following error:
Uncaught Exception: Object->__call(): the method 'category' does not exist on 'Page'
Trace
* Object->__call(Category,Array)
* Page->Category()
Line 26 of SiteTreeCategory.php
* SiteTreeCategory->updateCMSFields(FieldSet,,,,,,)
Line 741 of Object.php
* Object->extend(updateCMSFields,FieldSet)
Line 1328 of SiteTree.php
* SiteTree->getCMSFields()
Line 13 of Page.php
* Page->getCMSFields(CMSMain)
Line 390 of CMSMain.php

I don´t have any idea about the reason of this error. Can anyone help me?

Best regards,
chrclaus

Avatar
Hamish

Community Member, 712 Posts

17 July 2009 at 10:47am

What if you decorate Page, rather than SiteTree?

Avatar
chrclaus

Community Member, 29 Posts

17 July 2009 at 11:06am

Hi Hamish,

I want to filter the SiteTree in the admin section by the new attribute. On the other side, I am not sure, if this solves my problem basically. What happens with a

class MyPage extends Page
. Probably a similar error is shown.

I don´t understand, why the function is not visible/accessible in Page?

Best regards,
chrclaus

Avatar
Hamish

Community Member, 712 Posts

17 July 2009 at 11:32am

Edited: 17/07/2009 11:33am

Another option would be to change

$category = $this->owner->Category();

to

$id = $this->owner->ID;
$category = DataObject::get_one("Category", "SiteTreeID = '{$id}'");

It effectively bypasses the Category helper method that is failing. Note that you'll have to change 'SiteTreeID' to the appropriate column name.

Avatar
chrclaus

Community Member, 29 Posts

17 July 2009 at 7:40pm

Hi Hamish,

this was a very good tip! Thank you.

$cID = $this->owner->CategoryID;
$category = DataObject::get_by_id("Category", $cID);

solves it at the moment. But this is only a workaround. Does anybody else has an idea of the origin reason?

Best regards,
chrclaus