FIX: Only show table_name warning on dev/build

This commit is contained in:
Loz Calver 2017-12-11 17:32:13 +00:00
parent 9aadd8db38
commit 0e1753f33d
2 changed files with 36 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,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('<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.',
'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;
}