diff --git a/src/Core/DB.php b/src/Core/DB.php index 7a6ac70670..ca48c0cfe9 100644 --- a/src/Core/DB.php +++ b/src/Core/DB.php @@ -36,6 +36,7 @@ namespace App\Core; use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\NotFoundException; +use App\Util\Formatting; use Closure; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\ExpressionBuilder; @@ -109,7 +110,7 @@ class DB return array_search($class, self::$table_map); } - public static function getPKForClass(string $class) + public static function getPKForClass(string $class): array { return self::$class_pk[$class]; } @@ -162,7 +163,7 @@ class DB fn ($p) => (fn ($o) => $o instanceof $aliases[$p]), ), ); - } else if (($options['limit'] ?? null) === 1) { + } elseif (($options['limit'] ?? null) === 1) { return $results[0] ?? null; } else { return $results; @@ -320,6 +321,17 @@ class DB return $id; } + public static function refetch(Entity $ent): ?Entity + { + $pks = self::getPKForClass($ent::class); + $criteria = []; + foreach ($pks as $pk) { + $method = Formatting::snakeCaseToCamelCase("get_{$pk}"); + $criteria[$pk] = $ent->{$method}(); + } + return self::findOneBy($ent::class, $criteria); + } + /** * Intercept static function calls to allow referring to entities * without writing the namespace (which is deduced from the call