[SECURITY][DB] Make user register 'atomic', by using a single transaction for inserting all objects, to avoid partial inserts

This commit is contained in:
2021-04-23 12:54:25 +00:00
parent 77655c1248
commit 7a90e844b7
2 changed files with 26 additions and 11 deletions

View File

@@ -173,6 +173,24 @@ abstract class DB
return $repo->count($table, $criteria);
}
/**
* Insert all given objects with the generated ID of the first one
*/
public static function persistWithSameId(object $owner, object | array $others, ?callable $extra = null)
{
$conn = self::getConnection();
$metadata = self::getClassMetadata(get_class($owner));
$seqName = $metadata->getSequenceName($conn->getDatabasePlatform());
self::persist($owner);
$id = $conn->lastInsertId($seqName);
F\map(is_array($others) ? $others : [$others], function ($o) use ($id) { $o->setId($id); self::persist($o); });
if (!is_null($extra)) {
$extra($id);
}
self::flush();
return $id;
}
/**
* Intercept static function calls to allow refering to entities
* without writing the namespace (which is deduced from the call