Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Problems extending Member class

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

Page: 1 2
Go to End
Author Topic: 3315 Views
  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Problems extending Member class Link to this post

    I wrote a module and added a new member class. To do this, I wrote following line in the modules _config.php file:

       Object::useCustomClass( "Member", "CommunityMember" );

    The CommunityMember.php is located under the modules folder in code/members. The code is:

    <?php
    /**
    * Community Members extend basic member class
    */
    class CommunityMember extends Member
    {
       
       static $singular_name = "Community Mitglied";
       static $plural_name = "Community Mitglieder";

       static $db = array(
          "Street" => "Varchar(250)",
          "ZIP" => "Varchar(50)",
          "City" => "Varchar(250)",
          "EmailVerified" => "Boolean",
          "EmailVerifyHash" => "Varchar(250)",
          "MustChangePassword" => "Boolean"
       );
       
    }

    ?>

    I used to write a registration form to create members. To do this, I wrote

    ...
    $member = new CommunityMember();
          $form->saveInto( $member );
    ...
    inside the method called by form action. It seems alright, the members are created, I can view them in phpmyadmin. But I can not see them in the Backoffice (Tab Security/Group/Members). Does anyone have a idea what´s wrong? If recommended I also post the code of the registration form...

    Thanks a lot!

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Problems extending Member class Link to this post

    Subclassing Member isn't a good idea, because there's so many assumptions in the framework that assume "Member".

    The better way is to create a Member extension using DataObjectDecorator which effectively lets you add additional DB fields onto the existing Member table and create additional methods, without subclassing Member. Go here for more information:

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

    Cheers,
    Sean

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Problems extending Member class Link to this post

    Sean: then why is subclassing the Member class used in the SilverStripe book? You know, the "class Developer extends Member" example in the CRM chapter...

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Problems extending Member class Link to this post

    The book only used an example of using the Member subclass with ModelAdmin, and on more front end focused tasks like MultiForm and collections. It did not, however, demonstrate that you can use derived classes of Member with the Security section of the CMS.

    The real problem is that the MemberTableField class that sits within the Security tab of the CMS is only designed to show Member records - not subclassed ones as well. This is generally not a problem, but because you add additional database fields to a Member subclass, this subclass will get it's own database table, and MemberTableField will no longer be able to show both Member and Developer because Developer has it's own fields.

    There's also other places within the framework that use code like singleton('Member') or new Member() - obviously these wouldn't work for your derived classes of Member because it doesn't know about them.

    The alternative would be to use ModelAdmin to manage your Member subclasses instead. I believe you could add a Groups tab containing checkboxes of group membership that would allow you to manage the Member "Groups" relationship. Another would be to just decorate the existing Member class with additional fields, and use Groups to put them in different categories like "Staff" or "Community Member".

    Hope this helps!

    Cheers,
    Sean

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Problems extending Member class Link to this post

    Thanks for the insight, Sean, much appreciated!

    SR

  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Re: Problems extending Member class Link to this post

    Thanks for your answers and help! I know now, it is not recommended to sublcass the Member class. Some other forum entries wrote about it too. So I decided to decorate member class, but failed. I always got an session error with this. Maybe it´s because I have already extended Member Class and database relations between Member class and CommunityMember where not removed (even if I called dev/build. I wonder, if there is a way to reset database to only required tables and tablefields without lossing data?

  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Re: Problems extending Member class Link to this post

    I now created a decorater to the member class. Code as follows:

    <?php
    class MemberDecorator extends DataObjectDecorator {

       function augmentSQL(SQLQuery &$query) {}
       
       function extraStatics() {
          return array(
             'db' => array(
           "Street" => "Varchar(250)",
           "ZIP" => "Varchar(50)",
           "City" => "Varchar(250)",
           "EmailVerified" => "Boolean",
           "EmailVerifyHash" => "Varchar(250)",
           "MustChangePassword" => "Boolean"
    ),
          );
       }

       public function updateCMSFields( FieldSet &$fields )
       {
          $fields->addFieldToTab( "Root.Members", new TextField( "Street", "Straße" ) );
          $fields->addFieldToTab( "Root.Members", new TextField( "ZIP", "PLZ" ) );
          $fields->addFieldToTab( "Root.Members", new TextField( "City", "Ortschaft" ) );
          $fields->addFieldToTab( "Root.Members", new CheckboxField( "EmailVerified", "E-Mail Adresse verifiziert" ) );
          $fields->addFieldToTab( "Root.Members", new CheckboxField( "MustChangePassword", "Passwort bei Erstanmeldung ändern" ) );

       }

    }

    Inside _config.php:
    Object::add_extension( "Group", "GroupDecorator" );
    DataObject::add_extension( "Member", "MemberDecorator" );

    Again the same problem: I can not list registered members in backoffice. Confusing, because there is no error displayed or mailed...

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Problems extending Member class Link to this post

    Your member records should be in the database - in order to add them to a Group in the CMS it is necessary to type the first name in order to bring up an autocomplete list of people to add.

    I've attached a screenshot to describe what I'm saying

    Cheers,
    Sean

    3315 Views
Page: 1 2
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.