I'm studing DynamoDB but I don't understand how can I create a unique user's id foreach user. In SQL is simple, there is an int (AI) field. It'll be a int value because each user's profile url is like (very common form):
my-application-url/users/[:id_users]
ex. my-application-url/users/1/
My old save_users function is:
public function save_users($users){
if($users->id == 0){
$this->tableGateway()->save($users);
}else{
$this->tableGateway()->update($users, 'id_users' => (int) $users->id);
}
}
I use zend framework 2.
I could get the last registered user's id and increment it, but if I have 2 or more simultaneus registrations, I could get 2 or more identical user's id.
the users dynamodb table it also simple:
{
"id_users" Number: 1,
"firstname" String: "Alessandro",
"lastname" String: "Corradini",
"email" String: "my-email",
"password" String: "my-encrypted-password"
}
POSSIBLE SOLUTION
I need a UUID field into users table. The table 'll be like:
{
"UUID" String : <UUID-placeholder>,
"id_users" Number: 1,
"firstname" String: "Alessandro",
"lastname" String: "Corradini",
"email" String: "my-email",
"password" String: "my-encrypted-password"
}
For id_users
identifier, also unique, I'll generate a fixed lenght id_users
with this function below. Before saving user into register.php
, I'll check if is present into table (and email also!!), if is not, save user, else generate another unique id.
function randomId($length){
$result = mt_rand(1, 9);
for($i = 0; $i < ($length - 1); $i++) {
$result .= mt_rand(0, 9);
}
return $result;
}
For 16 character length, I have 10^16 combinations. I think that is enaugh.