Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update info for disabling "auto-tables" #7881

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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