Skip to content

Commit

Permalink
Merge pull request #7881 from cakephp/disable-auto-tables
Browse files Browse the repository at this point in the history
Update info for disabling "auto-tables"
  • Loading branch information
markstory authored Jul 1, 2024
2 parents a51255a + 9daf110 commit d3d710e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
15 changes: 14 additions & 1 deletion en/development/application.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ global event listeners::
// Call the parent to `require_once` config/bootstrap.php
parent::bootstrap();

// CakePHP has the ability to fallback to using the `Cake\ORM\Table`
// class to represent your database tables when a related class is
// not created for that table. But using this "auto-tables" feature
// can make debugging more difficult in some scenarios. So we disable
// this feature except for the CLI environment (since the classes
// would not be present when using the `bake` code generation tool).
if (PHP_SAPI !== 'cli') {
FactoryLocator::add(
'Table',
(new TableLocator())->allowFallbackClass(false)
);
}

// Load MyPlugin
$this->addPlugin('MyPlugin');
}
Expand All @@ -74,4 +87,4 @@ each test method.

.. meta::
:title lang=en: CakePHP Application
:keywords lang=en: http, middleware, psr-7, events, plugins, application, baseapplication
:keywords lang=en: http, middleware, psr-7, events, plugins, application, baseapplication,auto tables,auto-tables,generic table,class
32 changes: 1 addition & 31 deletions en/development/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -564,36 +564,6 @@ The built in configuration engines are:

By default your application will use ``PhpConfig``.

Disabling Generic Tables
========================

While utilizing generic table classes - also called auto-tables - when quickly
creating new applications and baking models is useful, generic table class can
make debugging more difficult in some scenarios.

You can check if any query was emitted from a generic table class via DebugKit
via the SQL panel in DebugKit. If you're still having trouble diagnosing an
issue that could be caused by auto-tables, you can throw an exception when
CakePHP implicitly uses a generic ``Cake\ORM\Table`` instead of your concrete
class like so::

// In your bootstrap.php
use Cake\Event\EventManager;
use Cake\Http\Exception\InternalErrorException;

$isCakeBakeShellRunning = (PHP_SAPI === 'cli' && isset($argv[1]) && $argv[1] === 'bake');
if (!$isCakeBakeShellRunning) {
EventManager::instance()->on('Model.initialize', function($event) {
$subject = $event->getSubject();
if (get_class($subject) === 'Cake\ORM\Table') {
$msg = sprintf(
'Missing table class or incorrect alias when registering table class for database table %s.',
$subject->getTable());
throw new InternalErrorException($msg);
}
});
}

.. meta::
:title lang=en: Configuration
:keywords lang=en: finished configuration,legacy database,database configuration,value pairs,default connection,optional configuration,example database,php class,configuration database,default database,configuration steps,index database,configuration details,class database,host localhost,inflections,key value,database connection,piece of cake,basic web,auto tables,auto-tables,generic table,class
:keywords lang=en: finished configuration,legacy database,database configuration,value pairs,default connection,optional configuration,example database,php class,configuration database,default database,configuration steps,index database,configuration details,class database,host localhost,inflections,key value,database connection,piece of cake,basic web

0 comments on commit d3d710e

Please sign in to comment.