Jump to:

3459 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Extend SiteTree using DataObjectDecorator

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2276 Views
  • chrclaus
    Avatar
    Community Member
    29 Posts

    Extend SiteTree using DataObjectDecorator Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Extend SiteTree using DataObjectDecorator Link to this post

    What if you decorate Page, rather than SiteTree?

  • chrclaus
    Avatar
    Community Member
    29 Posts

    Re: Extend SiteTree using DataObjectDecorator Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Extend SiteTree using DataObjectDecorator Link to this post

    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.

  • chrclaus
    Avatar
    Community Member
    29 Posts

    Re: Extend SiteTree using DataObjectDecorator Link to this post

    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

    2276 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.