I'm using drupal_exectue('user_register', $fields) to create user profiles (including the content_profile module). But I need to know the uid of the newly added account after the call to drupal_execute. Is there an easy way to do that?

I've tried db_last_insert_id('user', 'uid') but I get strange results. I suppose that's because inserts on other tables have occurred after the insert on {users}. If db_last_insert_id always returns the id of the *last* table accessed, what's the point of the two parameters for db_last_insert_id - table and field?

Comments

justageek’s picture

And I know based on your other posts what problem you are trying to solve with Ubercart. Content Profile will forward a user during their first login to the necessary form to create their profile if it does not exist, do you absolutely need to the Profile to be created during checkout?

extexan’s picture

Yes, we absolutely need it. This is a site selling content - not physical products. Consider this situation...

As an anonymous visitor, I "buy" some content. Ubercart creates the user record (old way, without content profile extras) to attach the order to. In that process, I have entered "my favorite username". Then I get a welcome email with instructions on how to create my *real* account (the one WITH content profile stuff included so I can actually use the site). At first, I'm a confused "customer" because I don't understand why I have to create another account when one was created for me during checkout, but I go along with it anyway. While creating my second account, I try to use "my favorite username" again but it tells me that one has already been used. :(

It's working already, like this... I save the new uid in a $_SESSION var in hook_user ($op = 'insert'), then when it gets to the user_save in Ubercart (which I replaced with my drupal_execute), I do a user_load after the drupal_execute with the uid from $_SESSION and assign that to $account so Ubercart is happy. It just seems like a kludge, so I was hoping for a more elegant way. But I guess if you consider I've already hacked Ubercart to begin with, what's a little more kludge, eh? :p

Btw, justageek, let me take this opportunity to say that those of us in need really appreciate those of you who volunteer your time to read and respond to these threads. Good job! And many thanks!

It's never too late to have a happy childhood. ;-)

extexan’s picture

After re-reading your post, I understand now what you were explaining about Content Profile and the first login. This is a client's site, and my first encounter with Content Profile. To be honest, this site has suffered from "too many fingers in the pie" and is, in itself, somewhat of a kludge. In my tests, I didn't experience what you described - perhaps because of some other "magic", whether intentional or accidental, that is overriding the redirection that CP is attempting. I'll check into that when I have more than two minutes to rub together.

It's never too late to have a happy childhood. ;-)

justageek’s picture

some of this can depend on the version of the module, what I did was download the latest content profile and look through the source code to get an idea of how it works. One module defines a Drupal 'rule' using the Drupal 6 rules system, and it basically check to see if the user is logging in for the first time and redirects them to the 'create profile' area if a profile node does not exist. Now, one possibility is that the rule is provided but must be configured in the rules admin area.

I just don't have a good test site right now to try it out.

I hope you find a better solution, and I hope I help some.

I wonder if you can just save their fav username in the order notes, then modify the default outgoing email and just tell them to login with that username? Does that sound like a possible solution?

extexan’s picture

Now that I know what Content Profile is supposed to do, I'll delve deeper into it to see why it's not doing that in this particular site.

And you helped more than you know. Thanks!

It's never too late to have a happy childhood. ;-)

Michael Zetterberg fd. Lopez’s picture

I had this exact same problem just now and this question was ranked high on google. So I thought I would share my solution for future visitors:

In user_register_submit the passed form state variable receives a reference to the created $account.

$form_state['user'] = $account;

so

// Make a form_state with values and stuff
$state = array(
  'values' => array(
    ...
  ),
);

drupal_execute('user_register', $state);

$uid = $state['user']->uid; // $state['user'] is the newly saved user account from user_save in user_register_submit
DrCord’s picture

Thank you very much for your post...knowing the $form_state grabs all the info about the newly created user is very helpful!

timo vogt’s picture

Dont know if this is solved!
But here is a little cheaty way you could do it.

<?php

$email =  $fields['mail'];
$account = user_load_by_mail($email);
$uid = $account['uid']

?>

Dont know why your using the drupal_execute function to save your user.
But I think this is the best way you can use to get the uid.