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   282 Views

Avatar
SonikArc

7 July 2014 at 11:24pm (Last edited: 7 July 2014 11:25pm), Community Member, 2 Posts

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

10 July 2014 at 10:49pm Community Member, 10 Posts

Hi there,

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

Avatar
SonikArc

10 July 2014 at 11:34pm Community Member, 2 Posts

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

12 July 2014 at 1:02pm Forum Moderator, 5511 Posts

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

12 August 2014 at 8:44am Community Member, 8 Posts

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.