General developer forum

Undefined property: stdClass::$username

 
Picture of Robert G
Undefined property: stdClass::$username
 

Hi,

I have an enrollment plugin that generates unique course enrollment keys. Whenever I generate keys, i get the following notice...

Notice: Undefined property: stdClass::$username in C:\inetpub\mysite-dev.xrite.com\moodle\lib\moodlelib.php on line 5924

That's a core Moodle file though, any idea how I would go about fixing this? If I remove the line the problem goes away, but I'm guessing something else in Moodle relies on this.

I'm guessing the error is coming from the plugin. Is it from my database query?

 
Average of ratings: -
Picture of Sam Chaffee
Re: Undefined property: stdClass::$username
Core developers

Hi,

What version of Moodle are you using? Do you have debugging turned up to Developer? Likely that will give you a stack-trace and you can see what is calling that core code and figure out why the username property is missing there.

 
Average of ratings: -
Picture of Robert G
Re: Undefined property: stdClass::$username
 

I have Moodle 3.3.2+ (Build: 20171013). I do have Developer debug turned on. I've had no problem getting stack traces for a couple of other errors I've encountered in the past, but this one isn't giving me much more to go off of. Everything still works as it should, I just don't like that the notice is being displayed.

Thanks,

 
Average of ratings: -
Picture of Sam Chaffee
Re: Undefined property: stdClass::$username
Core developers

If this is on your development site you could try dumping the $user object with print_r or var_dump and see why the username isn't set.

 
Average of ratings: Useful (1)
Picture of Robert G
Re: Undefined property: stdClass::$username
 

Thanks for the guidance, I'll take look when I get back to the office.

 
Average of ratings: -
Picture of Darko Miletić
Re: Undefined property: stdClass::$username
Core developersParticularly helpful Moodlers

You are using Moodle 3.3 and the code in question is located here:

https://github.com/moodle/moodle/blob/MOODLE_33_STABLE/lib/moodlelib.php#L5924

5922    if ($user->id > 0) {
5923        $context['touserid'] = $user->id;
5924        $context['tousername'] = $user->username; // <-- Line in question.
5925    }


This is located in function email_to_user. This essentially means that user object does not have username value. Why? You will have to figure that one.





 
Average of ratings: -