2016-02-21 50 views
1

我正在嘗試創建一個新的Drupal 7用戶,然後將該用戶的UID分配給一個變量。我想存儲這個UID,以便我可以創建一個新節點並將該用戶分配爲所有者。以編程方式創建Drupal用戶,然後存儲新用戶的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

怎麼叫數據庫或模型的UID?它實際上是UID還是僅僅是id? –

+0

它實際上是UID。 – sephiith

回答

1

user_save()返回在成功保存用戶對象(變量由值發送,而不是由參考)。試試這個:

$account = user_save($newUser); 
$uidn = $account->uid; 
+0

當我添加$帳戶= user_save($ newUser);到我的代碼我得到下面的PHP錯誤。 PDOException:SQLSTATE [23000]:完整性約束違規:1062鍵'PRIMARY'的重複條目'0':INSERT INTO {users}(name,pass,mail,status,init)VALUES(:db_insert_placeholder_0,:db_insert_placeholder_1,: db_insert_placeholder_2,:db_insert_placeholder_3,:db_insert_placeholder_4);數組([:db_insert_placeholder_0] => namec [:db_insert_placeholder_1] => test [:db_insert_placeholder_2] => [email protected] [:db_insert_placeholder_3] => 1 [:db_insert_placehold ........ – sephiith

+0

它也值得注意到這個PHP正在規則模塊中得到執行。 – sephiith

相關問題