Skip to content
Snippets Groups Projects
Commit 26af025d authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

sorm only return new ids for md5 hashes, fixes #4586

Closes #4586

Merge request studip/studip!3396
parent afad197d
No related branches found
No related tags found
No related merge requests found
...@@ -1267,20 +1267,29 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -1267,20 +1267,29 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} }
/** /**
* create new unique pk as md5 hash * Create new unique pk as md5 hash
* if pk consists of multiple columns, false is returned *
* @return boolean|string * This will only work for said md5 hashes columns. An exception is thrown
* otherwise.
*
* @return string
*/ */
function getNewId() public function getNewId()
{ {
$id = false; if ($this->hasAutoIncrementColumn()) {
if (count($this->pk()) == 1) { 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 { do {
$id = md5(uniqid($this->db_table(), 1)); $id = md5(uniqid($this->db_table(), true));
$db = DBManager::get()->query("SELECT `{$this->pk()[0]}` FROM `{$this->db_table()}` " $db = DBManager::get()->query("SELECT `{$this->pk()[0]}` FROM `{$this->db_table()}` "
. "WHERE `{$this->pk()[0]}` = '$id'"); . "WHERE `{$this->pk()[0]}` = '$id'");
} while ($db->fetch()); } while ($db->fetch());
}
return $id; return $id;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment