From 8317c612ff364fa1bfe7a305861d131464868d43 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 21 Jul 2021 16:40:48 +0000 Subject: [PATCH] [DB] Handle using methods with class name as well as table name and add lookup methods --- src/Core/DB/DB.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Core/DB/DB.php b/src/Core/DB/DB.php index 4ed986e035..5e88c6339e 100644 --- a/src/Core/DB/DB.php +++ b/src/Core/DB/DB.php @@ -34,7 +34,6 @@ namespace App\Core\DB; use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\NotFoundException; -use App\Util\Formatting; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Expr\Expression; use Doctrine\Common\Collections\ExpressionBuilder; @@ -55,14 +54,26 @@ abstract class DB * Table name to class map, used to allow specifying table names instead of classes in doctrine calls */ private static array $table_map = []; + private static array $class_pk = []; public static function initTableMap() { $all = self::$em->getMetadataFactory()->getAllMetadata(); foreach ($all as $meta) { - self::$table_map[$meta->getTableName()] = $meta->getMetadataValue('name'); + self::$table_map[$meta->getTableName()] = $meta->getMetadataValue('name'); + self::$class_pk[$meta->getMetadataValue('name')] = $meta->getIdentifier(); } } + public static function getTableForClass(string $class) + { + return array_search($class, self::$table_map); + } + + public static function getPKForClass(string $class) + { + return self::$class_pk[$class]; + } + /** * Perform a Doctrine Query Language query */ @@ -208,7 +219,7 @@ abstract class DB public static function __callStatic(string $name, array $args) { if (in_array($name, ['find', 'getReference', 'getPartialReference', 'getRepository']) - && !Formatting::startsWith($args[0], '\\')) { + && !str_contains($args[0], '\\')) { $args[0] = self::$table_map[$args[0]]; }