开发者

Drupal user_save() duplicate email address

开发者 https://www.devze.com 2023-01-31 07:02 出处:网络
I have a module where I try to create some users. $newUser = array( \'name\' => \"Bob\", 开发者_如何学Go\'pass\' => \"pass\",

I have a module where I try to create some users.

$newUser = array(
        'name' => "Bob",
  开发者_如何学Go      'pass' => "pass",
        'mail' => "a@a.com",
        'status' => 1,
        'init' => "a@a.com"
);
$newUserObject = user_save(null, $newUser1);

If a user is all ready created with the same mail address i am not getting false returned I get the user object who all ready existed. Is there any way I can be told that the user all ready exists.


You could try calling user_load with the email address before trying to create the user, to see if it returns a user object. Here's an example (borrowed from here):

//search by email
$account = user_load(array('mail' => check_plain($email)));
if ($account->uid) {
  //user found
} else {
  //user NOT found
}


You can try searching for the user before creating the user. Assuming drupal 6:

user_search('search', {email}, TRUE);

Or just query directly and see if the query returns FALSE.

db_result(db_query('SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s'), $email));

http://api.drupal.org/api/drupal/modules--user--user.module/function/user_search/6


To do this in drupal 7 you need to use different function, like this.

//search by email
$account = user_load_by_mail(check_plain($old_user['email_address']));
if ($account->uid) {
  //user found
} else {
  //user NOT found
}

as this is the function for D7

0

精彩评论

暂无评论...
验证码 换一张
取 消