- 🇨🇭Switzerland megadesk3000
I have the same issue still in the 2.x Version of entity_clone.
The issue I have is with cloning a user more than once.Steps to reproduce:
1. Create a user with name "user_original"
2. Clone the user the first time. No errors occur. The user gets cloned correctly.
3. Clone the "original" user once again. Now I get the "integrity constrained violation"Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user_original_cloned-de' for key 'user__name': INSERT INTO "users_field_data" ("uid", "langcode", "preferred_langcode", "preferred_admin_langcode", "name", "pass", "mail", "timezone", "status", "created", "changed", "access", "login", "init", "default_langcode") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14);
I w ill try to provide a patch, that makes the cloned username unique by adding a suffix to the cloned username if needed.
- Merge request !37Issue #3107686: Cloning original node multiple times with clone user enabled causes an error → (Merged) created by megadesk3000
- Status changed to Needs review
almost 2 years ago 9:03am 21 February 2023 - Status changed to Needs work
over 1 year ago 9:34am 25 March 2023 - 🇮🇳India rajeshreeputra Pune
Patch works and I don't see the error anymore.
below code snippet from MR loops 'n' number of time for given username in db, instead have query to get latest(stored) user starts with $username.
ex. lwt say db has 5 user with as below- original
- original_cloned
- original_cloned_1
- original_cloned_2
- original_cloned_3
- original_cloned_4
here latest entry is original_cloned_4 then explode with '_' and get last item + 1, which will gives '5'.
@megadesk3000 what you say about this approach?
+ protected function getUniqueUserName($username) { + $storage = $this->entityTypeManager->getStorage('user'); + $original = $username; + + $suffix = 0; + while ($matches = $storage->loadByProperties(['name' => $username])) { + $username = $original . '_' . $suffix++; + } + + return $username; + } +
- Status changed to RTBC
8 months ago 7:57am 10 May 2024 - last update
8 months ago 30 pass -
Rajeshreeputra →
committed d8f0a788 on 2.x authored by
megadesk3000 →
Issue #3107686 by megadesk3000, kbrodej, Rajeshreeputra: Cloning...
-
Rajeshreeputra →
committed d8f0a788 on 2.x authored by
megadesk3000 →
Automatically closed - issue fixed for 2 weeks with no activity.