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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Problems extending Member class


Go to End


11 Posts   8431 Views

Avatar
eddieconnecti

Community Member, 26 Posts

5 February 2010 at 11:25am

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!

Avatar
Sean

Forum Moderator, 922 Posts

9 February 2010 at 3:23pm

Edited: 09/02/2010 3:25pm

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

Avatar
SilverRay

Community Member, 167 Posts

9 February 2010 at 3:46pm

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

Avatar
Sean

Forum Moderator, 922 Posts

9 February 2010 at 4:58pm

Edited: 09/02/2010 5:03pm

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

Avatar
SilverRay

Community Member, 167 Posts

9 February 2010 at 6:18pm

Thanks for the insight, Sean, much appreciated!

SR

Avatar
eddieconnecti

Community Member, 26 Posts

9 February 2010 at 9:03pm

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?

Avatar
eddieconnecti

Community Member, 26 Posts

10 February 2010 at 8:41am

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

Avatar
Sean

Forum Moderator, 922 Posts

10 February 2010 at 10:06am

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

Go to Top