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.

Archive /

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

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

Extend Member using DataObjectDecorator


Reply


6 Posts   4138 Views

Avatar
g

Community Member, 22 Posts

3 December 2008 at 10:03am

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

Avatar
Nivanka

Community Member, 400 Posts

3 December 2008 at 3:20pm

check the forum's ForumRole.php

Avatar
Liam

Community Member, 470 Posts

3 December 2008 at 6:12pm

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

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

Avatar
g

Community Member, 22 Posts

6 December 2008 at 6:50am

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>" );
      }
   }

Avatar
adiwidjaja

Community Member, 14 Posts

9 December 2008 at 11:59pm

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');

Avatar
g

Community Member, 22 Posts

10 December 2008 at 4:31am

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?