Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Extend Member using DataObjectDecorator

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 4084 Views
  • g
    Avatar
    Community Member
    22 Posts

    Extend Member using DataObjectDecorator Link to this post

    Can anyone give me a code example or a link showing how to extend a member object to have additional fields using a DataObjectDecorator? As simple as you have got.
    Thanks

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Extend Member using DataObjectDecorator Link to this post

    check the forum's ForumRole.php

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Extend Member using DataObjectDecorator Link to this post

    Have you checked the docs? A lot of info in there.

    http://doc.silverstripe.com/doku.php?id=dataobjectdecorator

  • g
    Avatar
    Community Member
    22 Posts

    Re: Extend Member using DataObjectDecorator Link to this post

    In the ForumRole decorator there is reference to a table ForumMember in a function called augmentDatabase.

    I don't see that table. What is this all about?

    function augmentDatabase() {
          $exist = DB::query( "SHOW TABLES LIKE 'ForumMember'" )->numRecords();
          if( $exist > 0 ) {
             DB::query( "UPDATE `Member`, `ForumMember` " .
                "SET `Member`.`ClassName` = 'Member'," .
                "`Member`.`ForumRank` = `ForumMember`.`ForumRank`," .
                "`Member`.`Occupation` = `ForumMember`.`Occupation`," .
                "`Member`.`Country` = `ForumMember`.`Country`," .
                "`Member`.`Nickname` = `ForumMember`.`Nickname`," .
                "`Member`.`FirstNamePublic` = `ForumMember`.`FirstNamePublic`," .
                "`Member`.`SurnamePublic` = `ForumMember`.`SurnamePublic`," .
                "`Member`.`OccupationPublic` = `ForumMember`.`OccupationPublic`," .
                "`Member`.`CountryPublic` = `ForumMember`.`CountryPublic`," .
                "`Member`.`EmailPublic` = `ForumMember`.`EmailPublic`," .
                "`Member`.`AvatarID` = `ForumMember`.`AvatarID`," .
                "`Member`.`LastViewed` = `ForumMember`.`LastViewed`" .
                "WHERE `Member`.`ID` = `ForumMember`.`ID`"
             );
             echo("<div style=\"padding:5px; color:white; background-color:blue;\">" . _t('ForumRole.TRANSFERSUCCEEDED','The data transfer has succeeded. However, to complete it, you must delete the ForumMember table. To do this, execute the query \"DROP TABLE \'ForumMember\'\".') . "</div>" );
          }
       }

  • adiwidjaja
    Avatar
    Community Member
    14 Posts

    Re: Extend Member using DataObjectDecorator Link to this post

    I have a much simpler example:

    ===mysite/code/MyMemberExtension===
    <?php
    class MyMemberExtension extends DataObjectDecorator {
       
       function extraDBFields() {
          return array(
             'db' => array(
                'Website'=>'Varchar'
             ),
             'has_one' => array(
                'Avatar' => 'Image'
             ),
          );
       }
       
       public function updateCMSFields(FieldSet &$fields) {
        $fields->push(new TextField('Website', 'Website-URL'));
        $fields->push(new ImageField('Avatar', 'Profile Image'));
       }   
    }
    ?>
    ===

    In mysite/_config.php:

    Object::add_extension('Member', 'MyMemberExtension');

  • g
    Avatar
    Community Member
    22 Posts

    Re: Extend Member using DataObjectDecorator Link to this post

    Thanks for responding adiwidjaja.

    That is a much simpler example. Also pointing out the _config.php inclusion is good too as this is how the system knows of the decorator's existence. The forum code takes days to ponder.

    Do you happen to know what is going on in the ForumRole function augmentDatabase() and the ForumMember table reference?

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