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.

Data Model Questions /

Can't seem to extend Member


Reply


5 Posts   378 Views

Avatar
SonikArc

Community Member, 2 Posts

7 July 2014 at 11:24pm

Edited: 07/07/2014 11:25pm

I'm trying to extend the Member class with a DataExtension as it recommends in the docs and I've managed to attach my DataExtension to the Member class however I cannot access my newly defined methods on the Member object.

MemberExtension.php

<?php

/**
* Extends the standard SilverStripe Member DataObject
*/
class MemberExtension extends DataExtension {
   private static $db = array(
      'MemberExpires' => 'Date',
      'Username' => 'Varchar'
   );

   /**
    * Returns the Member Object for the username specified
    *
    * @param $id int the username
    * @returns Member|null the Member object or null if not found
    */
   public static function getByUsername(string $username) {
      return Member::get()->filter('Username', $username)->first();
   }
}

I'm adding the extension in the YAML config, I do a /dev/build and the new columns appear in the DB.
When I run

Object::get_extensions('Member')

it properly returns an array with my MemberExtension class.

However whenver I run

Member::getByUsername()

it always fails citing a "Fatal error: Call to undefined method Member::getByUsername()"

Am I doing this properly?
Is their anything I'm not grasping?

Thanks for your time.

Avatar
senorgeno

Community Member, 10 Posts

10 July 2014 at 10:49pm

Hi there,

Static functions aren't available on the extended class. You will need to call MemberExtension::getByUsername($username);

Avatar
SonikArc

Community Member, 2 Posts

10 July 2014 at 11:34pm

Has this always been the case or is it something that's happened in SilverStripe 3? Because if I can remember correctly I used to be able to have static functions in DataObjectDecorator's in SilverStripe 2.

So what would be the proper way to create this method, getByUsername(). It should obviously be a static method but is their somewhere else it should go instead of in a DataObject? Should I extend the Member class instead of creating a DataExtension? What's the solution here?

Avatar
Willr

Forum Moderator, 5513 Posts

12 July 2014 at 1:02pm

So what would be the proper way to create this method, getByUsername(). It should obviously be a static method but is their somewhere else it should go instead of in a DataObject? Should I extend the Member class instead of creating a DataExtension? What's the solution here?

It's fine to be on the extension. You just need to make sure you refer to the method call as MemberExtension::get_by_username($name) (note it's SilverStripe convention to use lower_camel_case for static methods).

Subclassing member will bring with it it's own issues so you're best to leave it on the extension.

Avatar
Mia

Community Member, 8 Posts

12 August 2014 at 8:44am

This may be a stupid reply, but I am not sure if you have added

"Member::add_extension('MemberDecorator');"

to your config.

Perhaps when you call get_extentions() it actually picks up that you have extended Member, without having added the look-up in config.php.