Skip to content

Commit

Permalink
BUG Added warning for auto-generated table_name for non-test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
flamerohr committed Nov 27, 2017
1 parent 7c39973 commit cc72b5c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/ORM/DataObjectSchema.php
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -126,6 +127,7 @@ public function tableName($class)
}
return null;
}

/**
* Returns the root class (the first to extend from DataObject) for the
* passed class.
Expand Down Expand Up @@ -301,10 +303,25 @@ protected function buildTableName($class)
$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;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/php/Core/ClassInfoTest/ChildClass.php
Expand Up @@ -2,7 +2,9 @@

namespace SilverStripe\Core\Tests\ClassInfoTest;

class ChildClass extends BaseClass
use SilverStripe\Dev\TestOnly;

class ChildClass extends BaseClass implements TestOnly
{

}

0 comments on commit cc72b5c

Please sign in to comment.