<p>Assuming this is Craft 4, it's actually possible to programmatically save a user without an email address (they will automatically be an <a href="https://craftcms.com/docs/4.x/users.html#active-and-inactive-users" rel="nofollow noreferrer">inactive user</a>).</p>
<p>But to your question, no - you can't get the ID before the user is saved (because it doesn't exist yet in the database).</p>
<p>An alternative approach could be to use user UIDs instead of IDs – because unlike IDs, you can safely create a UID yourself, set it to the user model being saved, and Craft will respect that UID and use it for the new user's <code>uid</code> database table column when it's saved.</p>
<p>Additionally, by using UIDs instead of IDs, you avoid a potential security issue in exposing the sequential primary key (i.e. the ID), in the event that these emails are ever displayed publicly.</p>
<pre class="lang-php prettyprint-override"><code>use Craft;
use craft\helpers\StringHelper;
use craft\elements\User;

...

$user = new User();
$user-&gt;uid = StringHelper::UUID();
$user-&gt;email = $user-&gt;uid . '@mysite.com';

$success = Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>

CC BY-SA 4.0
2023-06-15T10:41:36.300Z
Thank you! This works great.
supazu
CC BY-SA 4.0
2023-06-15T18:55:22.780Z
CraftCMS can be configured to use a Postgresql database. In Postgresql getting the next ID and saving the row to the database are 2 different operations (usually done at the same time via a default value in the primary key field). Technicallt, you can always ask the database for the next ID and then make a user with that specific id
Ferrybig
CC BY-SA 4.0
2023-06-15T21:43:04.053Z
@Ferrybig Myeah, you're not going to be able to "make a user with [a] specific id", regardless of the database engine – at least not going through the core Craft APIs (i.e. `Craft::$app->getElements()->saveElement()`). That said, you're certainly correct that it's technically possible to figure out what the new user's ID _is going to be_, using either database engine (the IDs are sequential, after all).
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2023-06-15T08:11:32.923Z
2023-06-15T09:37:57.393Z
2023-06-15T09:37:57.393Z
Mats Mikkel Rummelhoff
<p>It's impossible to get the user ID before the user is saved, because the ID is returned by the database, and that only happens once the user has been saved for the first time.</p>
<p>If you really <em>need</em> the emails in this format, you could:</p>
<ol>
<li>Set the email to a temporary, randomly generated address:<br />
<code>$user-&gt;email = uniqid() . '@mysite.com'</code></li>
<li>Save the user.</li>
<li>Get the ID and set the e-mail to your desired format:<br />
<code>$user-&gt;email = $user-&gt;id . '@mysite.com'</code></li>
<li>Save the user again.</li>
</ol>
<p>However, consider if you really need steps three and four. The email doesn't work anyway, so is there any benefit to having the user ID in place of a random string?</p>

CC BY-SA 4.0
2023-06-15T10:41:19.767Z
Thanks for the reply! I'd mark this as the answer as well if I could.
supazu
CC BY-SA 4.0
2023-06-15T07:59:45.297Z
2023-06-15T07:59:45.297Z
MoritzLost
<p>I'm creating users programmatically, and in some cases, they won't have an email address. In these cases, I would like to just set the email address to <code>1234@mysite.com</code> where <code>1234</code> is the User ID.</p>
<p>It's completely illogical, but is it possible to get the User ID at this point? Is there anything with dirty fields I can use? Otherwise, any suggestions on what I could use?</p>

<modules>
Get User ID before the user is created
# Get User ID before the user is created

(Asked by: username_0 on 2023-06-15)

I'm creating users programmatically, and in some cases, they won't have an email address. In these cases, I would like to just set the email address to `1234@mysite.com` where `1234` is the User ID.

It's completely illogical, but is it possible to get the User ID at this point? Is there anything with dirty fields I can use? Otherwise, any suggestions on what I could use?

**Tags:** modules


---

## 2 answers

### Answer by username_1 on 2023-06-15. Score: 3 | Accepted answer

Assuming this is Craft 4, it's actually possible to programmatically save a user without an email address (they will automatically be an inactive user).
But to your question, no - you can't get the ID before the user is saved (because it doesn't exist yet in the database).
An alternative approach could be to use user UIDs instead of IDs – because unlike IDs, you can safely create a UID yourself, set it to the user model being saved, and Craft will respect that UID and use it for the new user's uid database table column when it's saved.
Additionally, by using UIDs instead of IDs, you avoid a potential security issue in exposing the sequential primary key (i.e. the ID), in the event that these emails are ever displayed publicly.
use Craft;
use craft\helpers\StringHelper;
use craft\elements\User;

...

$user = new User();
$user->uid = StringHelper::UUID();
$user->email = $user->uid . '@mysite.com';

$success = Craft::$app->getElements()->saveElement($user);

> **Comments:**
> **username_0**: Thank you! This works great.  
> **username_2**: CraftCMS can be configured to use a Postgresql database. In Postgresql getting the next ID and saving the row to the database are 2 different operations (usually done at the same time via a default value in the primary key field). Technicallt, you can always ask the database for the next ID and then make a user with that specific id  
> **username_1**: @username_2 Myeah, you're not going to be able to "make a user with [a] specific id", regardless of the database engine – at least not going through the core Craft APIs (i.e. `Craft::$app->getElements()->saveElement()`). That said, you're certainly correct that it's technically possible to figure out what the new user's ID _is going to be_, using either database engine (the IDs are sequential, after all).  

---

### Answer by username_3 on 2023-06-15. Score: 3

It's impossible to get the user ID before the user is saved, because the ID is returned by the database, and that only happens once the user has been saved for the first time.

If you really *need* the emails in this format, you could:

1. Set the email to a temporary, randomly generated address:  
   `$user->email = uniqid() . '@mysite.com'`
2. Save the user.
3. Get the ID and set the e-mail to your desired format:  
   `$user->email = $user->id . '@mysite.com'`
4. Save the user again.

However, consider if you really need steps three and four. The email doesn't work anyway, so is there any benefit to having the user ID in place of a random string?

> **Comments:**
> **username_0**: Thanks for the reply! I'd mark this as the answer as well if I could.  

---

