From 0e1753f33d855dd1567a4ac416d6031aa5e994e2 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Mon, 11 Dec 2017 17:32:13 +0000 Subject: [PATCH 1/2] FIX: Only show table_name warning on dev/build --- src/ORM/Connect/DBSchemaManager.php | 31 +++++++++++++++++++++++++++++ src/ORM/DataObjectSchema.php | 14 +++++-------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/ORM/Connect/DBSchemaManager.php b/src/ORM/Connect/DBSchemaManager.php index 2eccafaa7..ddd3208ef 100644 --- a/src/ORM/Connect/DBSchemaManager.php +++ b/src/ORM/Connect/DBSchemaManager.php @@ -50,6 +50,20 @@ abstract class DBSchemaManager */ protected $supressOutput = false; + /** + * @var array + */ + protected static $table_name_warnings = []; + + /** + * @param string + * @deprecated 4.0..5.0 + */ + public static function showTableNameWarning($table, $class) + { + static::$table_name_warnings[$table] = $class; + } + /** * Injector injection point for database controller * @@ -409,6 +423,23 @@ abstract class DBSchemaManager $this->requireIndex($table, $indexName, $indexSpec); } } + + // Check and display notice about $table_name + static $table_name_info_sent = false; + + if (isset(static::$table_name_warnings[$table])) { + if (!$table_name_info_sent) { + $this->alterationMessage('Please note: It is strongly recommended to define a' . + ' table_name for all namespaced models. Not defining a table_name may cause generated 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.', + 'error' + ); + $table_name_info_sent = true; + } + + $this->alterationMessage('table_name not set for class ' . static::$table_name_warnings[$table], 'notice'); + } } /** diff --git a/src/ORM/DataObjectSchema.php b/src/ORM/DataObjectSchema.php index 18a616f59..6ad3cfe34 100644 --- a/src/ORM/DataObjectSchema.php +++ b/src/ORM/DataObjectSchema.php @@ -11,6 +11,7 @@ use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\TestOnly; +use SilverStripe\ORM\Connect\DBSchemaManager; use SilverStripe\ORM\FieldType\DBComposite; use SilverStripe\ORM\FieldType\DBField; @@ -317,18 +318,13 @@ class DataObjectSchema return $class; } - if (!ClassInfo::classImplements($class, TestOnly::class) && $this->classHasTable($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, '\\')); + if (!ClassInfo::classImplements($class, TestOnly::class) && $this->classHasTable($class)) { + DBSchemaManager::showTableNameWarning($table, $class); + } + return $table; } From 2391af5ba7f6851bbefa71bd835ed64e73ac198f Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 12 Dec 2017 09:22:18 +1300 Subject: [PATCH 2/2] Fix literal linting --- src/ORM/Connect/DBSchemaManager.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ORM/Connect/DBSchemaManager.php b/src/ORM/Connect/DBSchemaManager.php index ddd3208ef..4ec52cbd9 100644 --- a/src/ORM/Connect/DBSchemaManager.php +++ b/src/ORM/Connect/DBSchemaManager.php @@ -429,10 +429,14 @@ abstract class DBSchemaManager if (isset(static::$table_name_warnings[$table])) { if (!$table_name_info_sent) { - $this->alterationMessage('Please note: It is strongly recommended to define a' . - ' table_name for all namespaced models. Not defining a table_name may cause generated 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.', + $this->alterationMessage( + <<<'MESSAGE' +Please note: It is strongly recommended to define a +table_name for all namespaced models. Not defining a table_name may cause generated 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. +MESSAGE + , 'error' ); $table_name_info_sent = true;