diff --git a/src/Command/BakeCommand.php b/src/Command/BakeCommand.php index 930ac6b9..3f77ce01 100644 --- a/src/Command/BakeCommand.php +++ b/src/Command/BakeCommand.php @@ -27,6 +27,8 @@ use Cake\Core\ConventionsTrait; use Cake\Event\Event; use Cake\Event\EventManager; +use Cake\ORM\Exception\MissingTableClassException; +use Cake\ORM\Locator\TableLocator; use InvalidArgumentException; use function Cake\Core\pluginSplit; @@ -67,6 +69,31 @@ public static function defaultName(): string return 'bake ' . $name; } + /** + * @inheritDoc + */ + public function initialize(): void + { + parent::initialize(); + + $locator = $this->getTableLocator(); + + try { + $locator->get('NonExistingTable'); + $fallbackEnabled = true; + $locator->remove('NonExistingTable'); + } catch (MissingTableClassException $e) { + $fallbackEnabled = false; + } + + if (!$fallbackEnabled) { + // Use our own table locator with fallback classes + $locator = new TableLocator(); + $locator->allowFallbackClass(true); + $this->setTableLocator($locator); + } + } + /** * Handles splitting up the plugin prefix and classname. *