加载烘焙插件模型失败并显示 "Table class for alias Data.History could not be found"

Loading a baked Plugin Model fails with "Table class for alias Data.History could not be found"

我将模型烘焙到插件中,但无法加载。

我做了什么:

  1. 烘焙模型

    bin/cake bake model History --connection data --plugin Data --no-rules --no-validation --table elements_history
    

One moment while associations are detected.

Baking table class for History...

Creating file /var/www/app/plugins/Data/src/Model/Table/HistoryTable.php Wrote /var/www/app/plugins/Data/src/Model/Table/HistoryTable.php

Baking entity class for History...

Creating file /var/www/app/plugins/Data/src/Model/Entity/History.php Wrote /var/www/app/plugins/Data/src/Model/Entity/History.php

Baking test fixture for History...

Creating file /var/www/app/plugins/Data/tests/Fixture/HistoryFixture.php Wrote /var/www/app/plugins/Data/tests/Fixture/HistoryFixture.php Bake is detecting possible fixtures...

Baking test case for Data\Model\Table\HistoryTable ...

Creating file /var/www/app/plugins/Data/tests/TestCase/Model/Table/HistoryTableTest.php Wrote /var/www/app/plugins/Data/tests/TestCase/Model/Table/HistoryTableTest.php Done

  1. 通过$this->addPlugin('Data');

    加载了新创建的插件
  2. 已验证插件已通过 bin/cake plugin loaded

    加载
  3. 确认plugins/Data/src/Model/Table/HistoryTable.php存在

    root@debian:/var/www/app# cat plugins/Data/src/Model/Table/HistoryTable.php
    <?php
    declare(strict_types=1);
    
    namespace Data\Model\Table;
    
    use Cake\ORM\Query;
    use Cake\ORM\RulesChecker;
    use Cake\ORM\Table;
    use Cake\Validation\Validator;
    
    /**
     * History Model
     *
     * @method \Data\Model\Entity\History newEmptyEntity()
     * @method \Data\Model\Entity\History newEntity(array $data, array $options = [])
     * @method \Data\Model\Entity\History[] newEntities(array $data, array $options = [])
     * @method \Data\Model\Entity\History get($primaryKey, $options = [])
     * @method \Data\Model\Entity\History findOrCreate($search, ?callable $callback = null, $options = [])
     * @method \Data\Model\Entity\History patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
     * @method \Data\Model\Entity\History[] patchEntities(iterable $entities, array $data, array $options = [])
     * @method \Data\Model\Entity\History|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
     * @method \Data\Model\Entity\History saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
     * @method \Data\Model\Entity\History[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
     * @method \Data\Model\Entity\History[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
     * @method \Data\Model\Entity\History[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
     * @method \Data\Model\Entity\History[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
     *
     * @mixin \Cake\ORM\Behavior\TimestampBehavior
     */
    class HistoryTable extends Table
    {
        /**
         * Initialize method
         *
         * @param array $config The configuration for the Table.
         * @return void
         */
        public function initialize(array $config): void
        {
            parent::initialize($config);
    
            $this->setTable('elements_history');
            $this->setDisplayField('id');
            $this->setPrimaryKey('id');
    
            $this->addBehavior('Timestamp');
    
        }
    
        /**
         * Returns the database connection name to use by default.
         *
         * @return string
         */
        public static function defaultConnectionName(): string
        {
            return 'data';
        }
    }
    
  4. Added the plugin model 到任意控制器方法:

    • 或者$this->loadModel('Data.History');
    • TableRegistry::getTableLocator()->get('Data.History');
  5. 得到错误:

Table class for alias Data.History could not be found. Cake\ORM\Exception\MissingTableClassException CORE/src/ORM/Locator/TableLocator.php:245

我尝试了什么:

您知道问题是什么以及如何解决吗?我正在使用 CakePHP 4.2.4 和 Bake 2。

如果是手动创建的插件,您需要确保相应地更新 composer.json 的自动加载器配置并转储自动加载器,否则无法找到您的插件 类。

您的本地 Data 插件看起来像这样:

{
    // ...
    "autoload": {
        "psr-4": {
            // ...
            "Data\": "plugins/Data/src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            // ...
            "Data\Test\": "plugins/Data/tests/"
        }
    }
}

然后转储自动加载器:

composer dumpautoload

如果您使用 bake 创建插件,它会询问您是否应该修改您的 composer.json,如果您允许,它会相应地更新它并转储自动加载器。

另见