Jump to:

3417 Posts in 1053 Topics by 730 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObjectDecorator

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

Page: 1
Go to End
Author Topic: 422 Views
  • azt3k
    Avatar
    Community Member
    9 Posts

    DataObjectDecorator Link to this post

    Has anyone managed to develop an extension that uses relationships?

    I have tried to set up a dataobject decorator to allow comments to placed on multiple DataObjects. The result is something like this:

    class Commentable extends DataObjectDecorator{

       public function extraStatics() {
          return array(
             'has_many'    => array(
                'Comments'      => 'Comment'
             )
          );
       }

       public function updateCMSFields($fields) {
          
          if(get_class($fields->fieldByName('Root.Content')) == 'TabSet'){

             $fields->addFieldsToTab('Root.Content.Comments', $this->getCommentFields());

          }elseif(get_class($fields->fieldByName('Root')) == 'TabSet'){

             $fields->addFieldsToTab('Root.Comments', $this->getCommentFields());

          }elseif(get_class($fields) == 'FieldSet'){
             foreach($this->getCommentFields() as $f){
                $fields->push($f);
             }
          }
          
       }

       protected function getCommentFields() {

          $fields = array();

          // Create complex table field for managing attached comments
          $CommentsField = new HasManyDataObjectManager(
             $this->owner,
             'Comments',
             'Comment',
             array(
                'Created'            => 'Date Posted',
                'Comment'          => 'Comment'
             ),
             'getCMSFields_forPopup'
          );
          $CommentsField->setParentClass('DataObject');

          $fields[] = $CommentsField;

          return $fields;
       }

       // other code here

    }

    with an associated DataObject here:

    class Comment extends DataObject{
       
       public static $db = array(
          'Comment'   => 'HTMLText',
          'IsDeleted'   => 'Boolean'
       );

       public static $has_one = array(
          'OwnerObject'   => 'DataObject' // This line is fishy...
       );

       // other code here

    }

    Then add the extension to the Page class for argument's sake

    Object::add_extension('Page', 'Commentable');

    So the environment builder runs, tables get made - looks fine until I try to save a page - it throws an error - it seems like the dataobjectmanager / complex table get confused.

    The error is as follows:

    Error at \sapphire\core\Object.php line 724: Uncaught Exception: Object->__call(): the method 'setbyidlist' does not exist on 'DataObjectSet' (http://my.domain.tld/admin/EditForm)

  • Willr
    Avatar
    Forum Moderator
    5473 Posts

    Re: DataObjectDecorator Link to this post

    As an aside, there is a comments module which works on DataObjects (https://github.com/silverstripe/silverstripe-comments).

    422 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.