Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Solved: Decorator Classes with DataObjectManager

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 515 Views
  • Sewer
    Avatar
    Community Member
    9 Posts

    Solved: Decorator Classes with DataObjectManager Link to this post

    First of all, I'd like to say hello to all forum members.
    I am a graphic designer and I'm using SilverStripe for small company websites mostly.
    I found out that there are many alements repeating in each project, so I'd like to standarize them.
    So I decided to use DataObjectDecorators for things like animated slideshows, link groups on Home Page etc, which will be added or not.

    I don't know what am I doing wrong, but there are some problems with has_many relation.
    I have tho decorators - one is working, the other is not working.
    I'm pasting code and dev error.

    SlidesHolder with Slider - this is working

    <?php

    class SlidesHolder extends DataObjectDecorator {

       public function extraStatics(){
          return array(
             'has_many' => array(
                'Slides' => 'Slide'
             )
          );
       }

       public function updateCMSFields(FieldSet &$fields) {
          
          $t_slides = _t('Backend.SLIDES', 'Slideshow');
          
          $fields->addFieldToTab("Root.Content.$t_slides", new HeaderField('Title', 'Title'));
          
          $fields->addFieldToTab("Root.Content.$t_slides", new ImageDataObjectManager(
             $this->owner,
             'Slides',
             'Slide',
             'Photo',
             array(
                'Name'=>'Name'   
             ),
             'getCMSFields_forPopup',
             NULL,
             NULL
          ));
       }

    }

    <?php
    class Slide extends DataObject {
       
       static $db = array (
       'Name' => 'Varchar(255)',
       'Desc' => 'Text',
       'SortOrder' => 'Int'
       );
       
       static $has_one = array (
       'Photo' => 'Image',
       'PageLink' => 'SiteTree',
       'SlidesHolder' => 'SlidesHolder'
       );
       
       static $default_sort = "SortOrder ASC";
       
       public function getCMSFields_forPopup() {
          
          return new FieldSet (      
             new TextField('Name', 'Nazwa'),
             new ImageField('Photo', 'Zdjęcie'),
             new TextareaField('Desc', 'Opis'),
             new SimpleTreeDropdownField('PageLinkID', "Link do strony", 'SiteTree'),
             new NumericField ('SortOrder', 'Kolejność', '', 3)         
          );
       }      
       
    }

    Object::add_extension(SliderConfig::$PageToApply, 'SlidesHolder'); - in _config.php

    GraphicLinksHolder with Graphic link - this is not working properly. I can add items by DataObjectManager, but there is error when I try to edit them.
    Here's the code:

    <?php

    class GraphicLinksHolder extends DataObjectDecorator {

       public function extraStatics(){
          return array(
             'db' => array(
             ),
             
             'has_many' => array (
                'GraphicLinks' => 'GraphicLink'
             )
          );
       }

       public function updateCMSFields(FieldSet &$fields) {
          
          $t_graphiclinks = _t('Backend.GRAPHICLINKS', 'Graphic links');
          $t_links = _t('Backend.LINKS', 'Links');
          
          $fields->addFieldToTab("Root.Content.$t_links", new HeaderField('Title', "$t_graphiclinks"));
          $fields->addFieldToTab("Root.Content.$t_links", new DataObjectManager(
             $this->owner,
             'GraphicLinks',
             'GraphicLink',
             array(
                'Name' => 'Name'
             ),
             'getCMSFields_forPopup',
             NULL,
             NULL
          ));
       
       }

    }

    <?php

    class GraphicLink extends DataObject
    {
       static $db = array (
          'Name' => 'Text',
       );

       static $has_one = array (
          'GraphicLinksHolder' => 'GraphicLinksHolder',
          'Photo' => 'Image'
       );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new ImageField('Photo')
          );
       }
    }

    Object::add_extension('HomePage', 'GraphicLinksHolder'); - in _Config.php

    Error:

    [User Error] Couldn't run query: SELECT "GraphicLink"."ClassName", "GraphicLink"."Created", "GraphicLink"."LastEdited", "GraphicLink"."Name", "GraphicLink"."GraphicLinksHolderID", "GraphicLink"."PhotoID", "GraphicLink"."ID", CASE WHEN "GraphicLink"."ClassName" IS NOT NULL THEN "GraphicLink"."ClassName" ELSE 'GraphicLink' END AS "RecordClassName" FROM "GraphicLink" WHERE ("ParentID" = '1') Unknown column 'ParentID' in 'where clause'

  • Sewer
    Avatar
    Community Member
    9 Posts

    Re: Solved: Decorator Classes with DataObjectManager Link to this post

    I've found it out.
    Has_one relation in DataObject needs to point on decorated class, not to decorator itself.

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