diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-09-19 12:10:57 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2024-09-19 14:11:25 +0200 |
| commit | 852cd37f301b85ebea88c52af87d769e4facca47 (patch) | |
| tree | 33ecbfa5dcd040810d239af2cb68bf46d26e0b8c /lib/models/SimpleORMap.class.php | |
| parent | a5d438bd66e6df8928ebca74a1da552c4d02f174 (diff) | |
sorm only return new ids for md5 hashes, fixes #4586
Closes #4586
Merge request studip/studip!3396
Diffstat (limited to 'lib/models/SimpleORMap.class.php')
| -rw-r--r-- | lib/models/SimpleORMap.class.php | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php index 09f67b4..f9e0635 100644 --- a/lib/models/SimpleORMap.class.php +++ b/lib/models/SimpleORMap.class.php @@ -1267,20 +1267,29 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate } /** - * create new unique pk as md5 hash - * if pk consists of multiple columns, false is returned - * @return boolean|string + * Create new unique pk as md5 hash + * + * This will only work for said md5 hashes columns. An exception is thrown + * otherwise. + * + * @return string */ - function getNewId() + public function getNewId() { - $id = false; - if (count($this->pk()) == 1) { - do { - $id = md5(uniqid($this->db_table(), 1)); - $db = DBManager::get()->query("SELECT `{$this->pk()[0]}` FROM `{$this->db_table()}` " - . "WHERE `{$this->pk()[0]}` = '$id'"); - } while($db->fetch()); + if ($this->hasAutoIncrementColumn()) { + throw new Exception('You cannot retrieve the new id for an autoincrement column'); } + + if (count($this->pk()) !== 1) { + throw new Exception('You cannot retrieve a new id for a composite primary key'); + } + + do { + $id = md5(uniqid($this->db_table(), true)); + $db = DBManager::get()->query("SELECT `{$this->pk()[0]}` FROM `{$this->db_table()}` " + . "WHERE `{$this->pk()[0]}` = '$id'"); + } while ($db->fetch()); + return $id; } |
