我正在尝试创建一个新的Drupal 7用户,然后将该用户的UID分配给变量。我想存储此UID,以便我可以创建一个新节点并将该用户指定为所有者。
问题:有谁知道如何获取新创建的用户的UID?
我的代码:
// CREATE USER
$newUser = array(
'name' => $refereeUsername,
'pass' => $refereePassword,
'mail' => $refereeEmail,
'status' => 1,
'init' => $refereeEmail,
'roles' => array(
2 => 'authenticated',
53 => 'Referer',
),
);
user_save($usera, $newUser);
// MY ATTEMPT TO GET THE NEWLY CREATED USER ID
$uidn = $usera->uid;
// CREATE NODE
$node = new stdClass;
$node->type = 'referee';
$node->title = 'Referee report for xx';
$node->uid = $uidn;
$node->status = FALSE;
$node->field_testff['und'][0]['value'] = "testworked";
node_object_prepare($node);
node_save($node);
答案 0 :(得分:1)
user_save()
返回用户对象(变量按值发送,而不是按引用发送)。试试这个:
$account = user_save($newUser);
$uidn = $account->uid;
答案 1 :(得分:0)
要解决此问题,我使用了user_load_by_mail()函数。使用user_save()函数在我的代码中导致了完整性约束违规错误。
$accountb = user_load_by_mail($email);
$uidn = $accountb->uid;
文档: https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_mail/7