Doppelte Benutzernamen verhindern
This commit is contained in:
parent
7c8fa99fa8
commit
24fa5cebd3
3 changed files with 39 additions and 2 deletions
|
|
@ -17,6 +17,30 @@ class User extends Model
|
|||
return $row === null ? null : static::fromRow($row);
|
||||
}
|
||||
|
||||
/** Sucht einen Benutzer anhand des Namens (optional ohne eine bestimmte ID). */
|
||||
public static function byName(string $name, ?int $excludeId = null): ?static
|
||||
{
|
||||
$name = self::normalizeName($name);
|
||||
$sql = 'SELECT * FROM `users` WHERE name = ?';
|
||||
$params = [$name];
|
||||
|
||||
if ($excludeId !== null) {
|
||||
$sql .= ' AND id <> ?';
|
||||
$params[] = $excludeId;
|
||||
}
|
||||
|
||||
$sql .= ' LIMIT 1';
|
||||
$row = db()->fetchOne($sql, $params);
|
||||
|
||||
return $row === null ? null : static::fromRow($row);
|
||||
}
|
||||
|
||||
/** Vereinheitlicht Leerzeichen im Namen. */
|
||||
public static function normalizeName(string $name): string
|
||||
{
|
||||
return trim(preg_replace('/\s+/u', ' ', $name) ?? '');
|
||||
}
|
||||
|
||||
/** Admins und Mitglieder, die als Tour Guide wählbar sind. */
|
||||
public static function selectableGuides(): array
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue