Harmster Posted September 13, 2012 Share Posted September 13, 2012 Hey PW community, This will be my first post so please don't be to hard on me 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 More sharing options...
SiNNuT Posted September 13, 2012 Share Posted September 13, 2012 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!"; } 1 Link to comment Share on other sites More sharing options...
Harmster Posted September 13, 2012 Author Share Posted September 13, 2012 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 More sharing options...
Pete Posted September 13, 2012 Share Posted September 13, 2012 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(); } } 3 Link to comment Share on other sites More sharing options...
Harmster Posted September 13, 2012 Author Share Posted September 13, 2012 Saw that you did edit the post, It works now Big thanks all of you! Link to comment Share on other sites More sharing options...
SiNNuT Posted September 13, 2012 Share Posted September 13, 2012 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. 2 Link to comment Share on other sites More sharing options...
SiNNuT Posted September 13, 2012 Share Posted September 13, 2012 Maybe you've already seen it but this thread might be helpful in some ways. http://processwire.com/talk/topic/1716-integrating-a-member-visitor-login-form/#entry15919 Link to comment Share on other sites More sharing options...
Adam Kiss Posted September 13, 2012 Share Posted September 13, 2012 I think that this can be further simplified with: if (wire('users')->find("email={$username}, limit=1")->id){ //... } Link to comment Share on other sites More sharing options...
ryan Posted September 14, 2012 Share Posted September 14, 2012 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 More sharing options...
Adam Kiss Posted September 14, 2012 Share Posted September 14, 2012 Wasn't this changed so that limit=1 queries returns one page only, or do I confuse this with some other system? Link to comment Share on other sites More sharing options...
ryan Posted September 16, 2012 Share Posted September 16, 2012 Wasn't this changed so that limit=1 queries returns one page only, or do I confuse this with some other system? Must be another system? Link to comment Share on other sites More sharing options...
Pete Posted September 17, 2012 Share Posted September 17, 2012 Must be another system? Wordpress - he's been using Wordpress behind our backs! Shun the unbeliever! I jest, of course. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now