3070 Posts in 869 Topics by 651 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1921 Views |
-
Extend SiteTree using DataObjectDecorator

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.phpI don´t have any idea about the reason of this error. Can anyone help me?
Best regards,
chrclaus -
Re: Extend SiteTree using DataObjectDecorator

17 July 2009 at 10:47am
What if you decorate Page, rather than SiteTree?
-
Re: Extend SiteTree using DataObjectDecorator

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
. Probably a similar error is shown.class MyPage extends Page
I don´t understand, why the function is not visible/accessible in Page?
Best regards,
chrclaus -
Re: Extend SiteTree using DataObjectDecorator

17 July 2009 at 11:32am Last edited: 17 July 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.
-
Re: Extend SiteTree using DataObjectDecorator

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
| 1921 Views | ||
|
Page:
1
|
Go to Top |


