Jump to content

Recommended Posts

Posted

I cannot understand why i can't get role of user.

$userid = 40;
$role  = $roles->get("premium");
$uid = (int) $userid;
$u = $users->get($uid);
if ($u->hasRole($role)){
 ...stuff
}

i get a fatal error:

Method NullPage::hasRole does not exist or is not callable in this context

Posted

Shouldn't it be hasRole("string"); ?

You load the role as object and not as string, and hasRole expects the name (string).

Posted

NullPage::hasRole says to me there's no "userpage" found so its returning NullPage, and there's no method on NullPage.

Posted

This line failed to load a user:

$u = $users->get($uid);

User ID 40 doesn't exist in the system because anytime PW returns a NullPage that means it didn't find what you were looking for. The best way to check if you got a NullPage is to see if the returned has an ID. So your last section of code should be something like this:

if ($u->id && $u->hasRole($role)){
  ...stuff
}

Btw, ID 40 is the default ID for the 'guest' user account, so it would be unusual if that really is the ID of the user and it didn't load.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...