From 3661a81d00ecde8205d0834252a6e45a3ce9fe8a Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sat, 17 Feb 2018 00:42:29 +0100 Subject: [PATCH] Experimental fix for table name and alias resolution Relying on a local static variable is dangerous, because it is bound to the implementing class, but once set, it becomes fixed for all derived classes too. This is similar to the `wcf\system\SingletonFactory` work-around that uses a lookup table rather than LSB fields for the same reasons. --- .../files/lib/data/DatabaseObject.class.php | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/wcfsetup/install/files/lib/data/DatabaseObject.class.php b/wcfsetup/install/files/lib/data/DatabaseObject.class.php index d073c2ed8e..54b7f2e7fe 100644 --- a/wcfsetup/install/files/lib/data/DatabaseObject.class.php +++ b/wcfsetup/install/files/lib/data/DatabaseObject.class.php @@ -41,6 +41,24 @@ abstract class DatabaseObject implements IStorableObject { */ protected static $sortOrder = null; + /** + * The list of derived database table names based on the class name. + * + * WARNING: This is strictly an internal lookup table. DO NOT USE IT. + * + * @var string[] + */ + protected static $_derivedDatabaseTableName = []; + + /** + * The list of derived database table aliases based on the class name. + * + * WARNING: This is strictly an internal lookup table. DO NOT USE IT. + * + * @var string[] + */ + protected static $_derivedDatabaseTableAlias = []; + /** * object data * @var array @@ -134,12 +152,11 @@ abstract class DatabaseObject implements IStorableObject { return $classParts[0].WCF_N.'_'.static::$databaseTableName; } - static $databaseTableName = null; - if ($databaseTableName === null) { - $databaseTableName = $classParts[0].WCF_N.'_'.strtolower(implode('_', preg_split('~(?=[A-Z](?=[a-z]))~', array_pop($classParts), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); + if (!isset(self::$_derivedDatabaseTableName[$className])) { + self::$_derivedDatabaseTableName[$className] = $classParts[0].WCF_N.'_'.strtolower(implode('_', preg_split('~(?=[A-Z](?=[a-z]))~', array_pop($classParts), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); } - return $databaseTableName; + return self::$_derivedDatabaseTableName[$className]; } /** @@ -150,13 +167,13 @@ abstract class DatabaseObject implements IStorableObject { return static::$databaseTableName; } - static $databaseTableNameAlias = null; - if ($databaseTableNameAlias === null) { - $classParts = explode('\\', get_called_class()); - $databaseTableNameAlias = strtolower(implode('_', preg_split('~(?=[A-Z](?=[a-z]))~', array_pop($classParts), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); + $className = get_called_class(); + if (!isset(self::$_derivedDatabaseTableAlias[$className])) { + $classParts = explode('\\', $className); + self::$_derivedDatabaseTableAlias[$className] = strtolower(implode('_', preg_split('~(?=[A-Z](?=[a-z]))~', array_pop($classParts), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); } - return $databaseTableNameAlias; + return self::$_derivedDatabaseTableAlias[$className]; } /** -- 2.20.1