Jump to content

Custom User Class, username check.


Harmster
 Share

Recommended Posts

Hey PW community,

This will be my first post so please don't be to hard on me :mellow:

So here's the thing, I've been working with PW for 2 days now and I am trying to make a custom user model for the repetative actions throughout my website, ofcourse using the User from PW.

Now I want to register a new user into the user Pages (I think thats how you guys call it, not sure x3)

Well that works but now I want my class to check if the username already exists, and thats where I get stuck. Normally I would just run a query and see if it returns more then 0 but I am not sure what to use in a model.

This is kind of what I tried.

public function check_username($username){
 wire('users')->find('email=username');
 return $users;
}

Returns nothing, except for a error:

Error Exception: Duplicate entry 'harm-29' for key 'name_parent_id' (in /xxxxl/core/Database.php line 118)

#0 /homexxxxl/wire/core/Pages.php(533): Database->query('INSERT INTO pag...')

#1 [internal function]: Pages->___save(Object(User))

#2 /home/xxxxl/wire/core/Wire.php(271): call_user_func_array(Array, Array)

#3 /home/xxxxlklanten//wire/core/Wire.php(229): Wire->runHooks('save', Array)

#4 /homexxxxl/wire/core/Page.php(869): Wire->__call('save', Array)

#5 /home/xxxxl/wire/core/Page.php(869): Pages->save(Object(User))

#6 /home/xxxxl/site/modules/Users/.module(52): Page->save()

#7 /home/xxxxl/public_html/klanten//site/template

This error message was shown because you are logged in as a Superuser. Error has been logged.

And this:

public function check_username($username){
 $users = new User();
 $users->find('email=$username');
 return $users;
}

Returns a zero + the earlier mentioned message.

So could someone please tell me what my options here are?

Oh and the username = always the email address so dont worry about that. ;3

Greetings,

Harm.

Link to comment
Share on other sites

You want to check after some kind of form submission? Not sure but something like this?

$u = $users->get("email=$email");
if($u->id) {
echo "Already taken!";
}

yeah thats what I want, I think im just doing something wrong here because it is not working...

This is what i have now:

public function check_username($username){
    $users = new User();
    $u = $users->get("email=$username");
    if($u->id) {
	    return "Already taken!";
    }
   }

And i get this again:

Error Exception: Duplicate entry 'myemail-29' for key 'name_parent_id' (in wire/core/Database.php line 118)

#0 wire/core/Pages.php(533): Database->query('INSERT INTO pag...')

#1 [internal function]: Pages->___save(Object(User))

#2 l/wire/core/Wire.php(271): call_user_func_array(Array, Array)

#3 /wire/core/Wire.php(229): Wire->runHooks('save', Array)

#4 /wire/core/Page.php(869): Wire->__call('save', Array)

#5 /wire/core/Page.php(869): Pages->save(Object(User))

#6 /site/modules/Usersgivetoall.module(52): Page->save()

#7 //site/template

This error message was shown because you are logged in as a Superuser. Error has been logged.

I am sorry if I just dont see it or am really bad D:

(BTW it is a MODULE)

Link to comment
Share on other sites

You don't want this then as that creates a new user object before you've even checked for the duplicate user:

$users = new User();

You need to skip that line and check it first like this:

public function check_username($username){
   $u = wire('users')->get("email=$username");
   if($u->id) {
	 return "Already taken!";
   } else {
        // Then add your code to add the new user here, something like:
    $user = new User();
    $user->name = $username;
    $user->save();
   }
}
  • Like 3
Link to comment
Share on other sites

I would first check if the username/email isn't already taken. If it is not taken continue with creating a new user.

public function check_username($username) {
$u = $users->get("email=$username");
if($u->id) {
return "Already taken!";
} else {
$nu = new User();
$nu->name = $username;
$nu->save();
//continue with the new user and further code
}

Dammit! You gotta be quick on this forum! Pete beat me to it.

  • Like 2
Link to comment
Share on other sites

Adam actually that wouldn't work because find() returns a PageArray, not a Page. So you'd have to do find(...)->first()->id, but a better way would be to just use get() rather than find():

if(wire('users')->get("email=$username")->id) {
 // ...
}
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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