From cc72b5c85246f40531979c19e905cb956af6314e Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Mon, 27 Nov 2017 20:14:10 +1300 Subject: [PATCH] BUG Added warning for auto-generated table_name for non-test classes --- src/ORM/DataObjectSchema.php | 23 ++++++++++++++++++--- tests/php/Core/ClassInfoTest/ChildClass.php | 4 +++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/ORM/DataObjectSchema.php b/src/ORM/DataObjectSchema.php index 0944dd762..34890d759 100644 --- a/src/ORM/DataObjectSchema.php +++ b/src/ORM/DataObjectSchema.php @@ -6,6 +6,7 @@ use Exception; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Dev\TestOnly; use SilverStripe\ORM\FieldType\DBComposite; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; @@ -126,6 +127,7 @@ class DataObjectSchema } return null; } + /** * Returns the root class (the first to extend from DataObject) for the * passed class. @@ -301,11 +303,26 @@ class DataObjectSchema $table = Config::inst()->get($class, 'table_name', Config::UNINHERITED); // Generate default table name - if (!$table) { - $separator = DataObjectSchema::config()->uninherited('table_namespace_separator'); - $table = str_replace('\\', $separator, trim($class, '\\')); + if ($table) { + return $table; } + if (strpos($class, '\\') === false) { + return $class; + } + + if (!ClassInfo::classImplements($class, TestOnly::class)) { + trigger_error( + "It is recommended to define a table_name for your '$class'." . + ' Not defining a table_name may cause subsequent table names to be too long and may not be supported' . + ' by your current database engine, the generated naming scheme will also change when upgrading to' . + ' SilverStripe 5.0 and potentially break.', + E_USER_WARNING + ); + } + $separator = DataObjectSchema::config()->uninherited('table_namespace_separator'); + $table = str_replace('\\', $separator, trim($class, '\\')); + return $table; } diff --git a/tests/php/Core/ClassInfoTest/ChildClass.php b/tests/php/Core/ClassInfoTest/ChildClass.php index 663b9c0e4..c9d69b992 100644 --- a/tests/php/Core/ClassInfoTest/ChildClass.php +++ b/tests/php/Core/ClassInfoTest/ChildClass.php @@ -2,7 +2,9 @@ namespace SilverStripe\Core\Tests\ClassInfoTest; -class ChildClass extends BaseClass +use SilverStripe\Dev\TestOnly; + +class ChildClass extends BaseClass implements TestOnly { }