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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[Solved!]... does not have a method 'add_to_class'...


Go to End


11 Posts   2511 Views

Avatar
Vlad Belfort

Community Member, 55 Posts

12 April 2016 at 5:38am

Edited: 12/04/2016 9:21pm

This error is driving me nuts.

I don't know when this started happening nor remember exactly what I was working on around the time this started occurring so the question might be a little broad but any additional info you'd need let me know and I'll update this.

I have a class called UserManager which retrieves user data from a server and saves it into the CMSes database. The script works fine, I ran it, it collected the data and stored it nicely. But, now whenever I run UserManager or even navigate around some parts of the site I keep getting these in my error logs

[11-Apr-2016 17:15:11]
Warning at framework/core/Object.php line 642:
call_user_func() expects parameter 1 to be a valid callback,
class 'UserManager' does not have a method 'add_to_class'
(http://intranet32.local/Security/login?BackURL=/home/)

[11-Apr-2016 17:15:20]
Warning at framework/core/Object.php line 642:
call_user_func() expects parameter 1 to be a valid callback,
class 'UserManager' does not have a method 'add_to_class'
(http://intranet32.local/Security/LoginForm)

[11-Apr-2016 17:29:43]
Warning at framework/core/Object.php line 642:
call_user_func() expects parameter 1 to be a valid callback,
class 'UserManager' does not have a method 'add_to_class'
(http://intranet32.local/usermanager/updateAllUserData)

I haven't been able to find anything relating to this by searching around nor have I come across anyone who's experiencing similar issues. Could someone tell me wtf this is and what steps I could take to resolve this issue?

Avatar
martimiz

Forum Moderator, 1391 Posts

13 April 2016 at 8:45pm

Edited: 13/04/2016 8:46pm

You may have expected this answer - hard to tell without any more info :).

Like what version of SilverStripe are you on, the code of the UserManager class, or even a stack trace...

Have you anabled dev mode and made sure it displays warnings - and then run the script? The trace might give you a clue where this happens. As it is only a warning, it wouldn't necessarily stop your code from executing, so the result could still be fine...

Avatar
Vlad Belfort

Community Member, 55 Posts

14 April 2016 at 1:49am

I sure did aha just wasn't sure what info would've been relevant to this warning.

I'm running v3.2 and have dev mode enabled in the .yml file

Where would I need to add the stack trace? I had it in UserManager but it didn't really tell me anything (At least I think it didn't)

Here's my code (some bits have been omitted due to sensitive information)...

UserManager.php

class UserManager extends Page_Controller {

    private static $allowed_actions = array(
        'updateAllUserData'
    );

    # ...

    public function updateAllUserData(){

        $amanda = file('#');

        $data = json_decode($amanda[0], true);

        echo '<h1>Fetching and processing data...</h1>';

        foreach ($data as $id => $userData){

            if($userData['userName'] && $user = Member::get()->filter(array('Email' => $userData['userName']))->first()){

                $user->UpdateAllData($userData);
                echo '<p style="font-size: 16px;font-weight: normal;">Updated user : ' . $userData['userName'] . '</p>';
            }
            else {
                if(!ctype_upper(substr($userData['userName'], 0,1))){

                    // create a new user
                    $user = Member::create();
                    $user->UserAdminID = $id;
                    $user->write();

                    $user->UpdateAllData($userData);
                    echo '<p style="font-size: 16px;font-weight: bold;>Created user : ' . $userData['userName'] . '</p>';
                }
            }
        }
    }
    # ...
}

Then the 'updateAllData' method comes from...

UserDataManager.php

class UserDataManager extends DataExtension {

    # ...

    // partial copy from 2.4
    function UpdateAllData($data){

        //Email
        $this->owner->Email = $data['userName'];

        //First Name
        $this->owner->FirstName = $data['firstName'];

        //Last Name
        $this->owner->Surname = $data['lastName'];

        # ...

        // Sites
        $London = Site::get()->filter(array('Title' => 'London'));
        #...

        switch ($this->owner->Area){

            # ...

            default :
                $this->owner->SiteID = $London->ID;
                $this->owner->ForumRank = 'London';
                break;
        }

        $this->owner->addToGroupByCode('user', 'User');
        return $this->owner->write();
    }
}

Avatar
martimiz

Forum Moderator, 1391 Posts

18 April 2016 at 4:59am

What I meant is, if you have dev mode enabled, SilverStripe should send errors and warnings to the screen. You would see the error, and underneath it a trace showing you where the error came from, which sometimes helps. Maybe in your case displaying errors is disabled, which is good on production, though not very helpful on development...

Looking through your code I see no obvious reason for the warning....

The thing is - I had exactly the same one two days ago, while building an Extension class - so now it's personal :) ButI just couldn't grasp where it came from. It had something to do with the static Extension::add_to_class() function, that does... exactly nothing! It is supposedly called when the extension is added to 'a particular' class...

So I just rebuilt the thing and now the problem seems to be gone... But I may have a look again in the next few days, 'cause it's all a bit weird

Avatar
Vlad Belfort

Community Member, 55 Posts

18 April 2016 at 10:22pm

Edited: 18/04/2016 10:40pm

Ohh I getcha - I have dev mode enabled in the .yml file but from what you're saying it appears to be doing nothing... I'll probably switch it over to the .PHP config file after this warning pisses off otherwise it'll drive me even more insane than it currently is by bombarding my log files with the same message.

I'm sorry to hear you had the same problem but (a little selfishly) glad that I'm not the only one anymore because I can't find anything anywhere regarding this issue!

You mentioned you were building an Extension class but you no longer have this issue, do you mind sharing the code in your _config file? It could be that I missed something out somewhere because that part of the process was a little confusing for me, I might have missed or overlooked something...

This is how I linked up my extensions

app.yml

Director:
 enviroment_type: 'dev'
Member:
  extensions:
    - UserManager
DataList:
  extensions:
    - UserDataManager

config.yml

---
Name: mysite
After:
  - 'framework/*'
  - 'cms/*'
---
# YAML configuration for SilverStripe
# See http://doc.silverstripe.org/framework/en/topics/configuration
# Caution: Indentation through two spaces, not tabs
SSViewer:
  theme: 'two-three'
Member:
  extensions:
    - UserDataManager

Edit: I have a screenshot of the stack trace after I ran ?isDev=1 and got the type of output you mentioned - I can post that if it helps narrow down the cause?

Avatar
martimiz

Forum Moderator, 1391 Posts

19 April 2016 at 2:24am

I don't see anything strange in the way you regster your extensions. Looks a bit strange that UserDataManager is added to Member as well in the second config, but I suppose you want the two extensions added to Member, using two separate config.ymls...?

Anyway, that should be all, you shouldn't have to add anything further to _config.php or wherever.

The trace could help, just remove any secret content :)

Avatar
Vlad Belfort

Community Member, 55 Posts

19 April 2016 at 10:22pm

Edited: 19/04/2016 10:25pm

About the config files, do I only need to use one? As I previously mentioned that part did confuse me a little which may be where the error is coming from..?

Whats the difference between those to .yml files?

Stack trace (Image wouldnt load, pasted link to it instead)

http://share.pho.to/A7G4y/sc/original

Avatar
martimiz

Forum Moderator, 1391 Posts

20 April 2016 at 3:12am

The _config.php files used to be the place to set things up, mostly by using static setter or enable-like functions. Sometimes you still need to do it that way, but most settings can and should be done in thes yml files.

In priciple you could use any available .yml file, BUT... In your own modules you would probably use it's .yml file(s) for settings needed to get the module working. After that - and when using thirdparty modules - you would use your mysite/_config/config.yml for custom settings, to make sure they're not overwritten when you update a module.

You can create - and use - as many .yml files as you like, but most times one is quite enough :-) Sometimes it is important that a setting is set before or after another setting. In that case you can use named blocks and use 'before' or 'after' to define their place in the hierarchy:

something like:

---
Name: mysettings
After:
  - 'mysite'
---
Member:
  extensions:
    - UserManager
    - UserDataManager
DataList:
  extensions:
    - UserDataManager

Note that at any change in a yml file or private static variables, you need to ?flush
more about config here: https://docs.silverstripe.org/en/3.3/developer_guides/configuration/configuration/

Thanks for the trace - I'll try and look into it as soon as I can - things are a bit busy right now :(

Go to Top