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.

Data Model Questions

Extend SiteTree using DataObjectDecorator


Reply

5 Posts   2293 Views

Avatar
chrclaus

17 July 2009 at 7:34am Community Member, 29 Posts

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

17 July 2009 at 10:47am Community Member, 712 Posts

What if you decorate Page, rather than SiteTree?

Avatar
chrclaus

17 July 2009 at 11:06am Community Member, 29 Posts

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

17 July 2009 at 11:32am (Last edited: 17 July 2009 11:33am), Community Member, 712 Posts

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

17 July 2009 at 7:40pm Community Member, 29 Posts

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