Merge pull request #7691 from kinglozzer/warn-table-name

FIX: Only show table_name warning on dev/build
This commit is contained in:
Damian Mooyman 2017-12-12 10:02:11 +13:00 committed by GitHub
commit 6be554a29e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 9 deletions

View File

@ -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,27 @@ 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(
<<<'MESSAGE'
<strong>Please note:</strong> 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;
}
$this->alterationMessage('table_name not set for class ' . static::$table_name_warnings[$table], 'notice');
}
}
/**

View File

@ -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;
}