File tree Expand file tree Collapse file tree 1 file changed +13
-8
lines changed Expand file tree Collapse file tree 1 file changed +13
-8
lines changed Original file line number Diff line number Diff line change @@ -236,7 +236,7 @@ function (Filter\Condition $condition) {
236
236
$ contactId = $ this ->getContactId ($ identifier );
237
237
if ($ contactId !== null ) {
238
238
if (! empty ($ data ['username ' ])) {
239
- $ this ->assertUniqueUsername ($ data ['username ' ]);
239
+ $ this ->assertUniqueUsername ($ data ['username ' ], $ contactId );
240
240
}
241
241
242
242
$ db ->update ('contact ' , [
@@ -447,19 +447,24 @@ private function addContact(array $data): void
447
447
* Assert that the username is unique
448
448
*
449
449
* @param string $username
450
+ * @param int $contactId The id of the contact to exclude
450
451
*
451
452
* @return void
452
453
*
453
454
* @throws HttpException if the username already exists
454
455
*/
455
- private function assertUniqueUsername (string $ username ): void
456
+ private function assertUniqueUsername (string $ username, int $ contactId = null ): void
456
457
{
457
- $ user = Database::get ()->fetchOne (
458
- (new Select ())
459
- ->from ('contact ' )
460
- ->columns ('1 ' )
461
- ->where (['username = ? ' => $ username ])
462
- );
458
+ $ stmt = (new Select ())
459
+ ->from ('contact ' )
460
+ ->columns ('1 ' )
461
+ ->where (['username = ? ' => $ username ]);
462
+
463
+ if ($ contactId ) {
464
+ $ stmt ->where (['id != ? ' => $ contactId ]);
465
+ }
466
+
467
+ $ user = Database::get ()->fetchOne ($ stmt );
463
468
464
469
if ($ user !== false ) {
465
470
throw new HttpException (422 , 'Username already exists ' );
You can’t perform that action at this time.
0 commit comments