Cakephp 加载烘焙插件模型失败,返回“0”;别名数据的表类。找不到历史记录;

Cakephp 加载烘焙插件模型失败,返回“0”;别名数据的表类。找不到历史记录;,cakephp,cakephp-model,cakephp-4.x,cakephp-bake,Cakephp,Cakephp Model,Cakephp 4.x,Cakephp Bake,我将模型烘焙到插件中,但无法加载 我所做的: 烤模型 bin/cake bake model History --connection data --plugin Data --no-rules --no-validation --table elements_history 检测到关联时请稍候 历史烘焙桌课程 创建文件/var/www/app/plugins/Data/src/Model/Table/HistoryTable.php 编写了/var/www/app/plugins/Data

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

我所做的:
  • 烤模型

    bin/cake bake model History --connection data --plugin Data --no-rules --no-validation --table elements_history
    
  • 检测到关联时请稍候

    历史烘焙桌课程

    创建文件/var/www/app/plugins/Data/src/Model/Table/HistoryTable.php 编写了
    /var/www/app/plugins/Data/src/Model/Table/HistoryTable.php

    正在烘焙历史记录的实体类

    创建文件/var/www/app/plugins/Data/src/Model/Entity/History.php 编写了
    /var/www/app/plugins/Data/src/Model/Entity/History.php

    历史烘焙测试夹具

    创建文件/var/www/app/plugins/Data/tests/Fixture/HistoryFixture.php 编写了
    /var/www/app/plugins/Data/tests/Fixture/HistoryFixture.php
    烘焙正在检测可能的设备

    正在烘焙Data\Model\Table\HistoryTable的测试用例

    创建文件/var/www/app/plugins/Data/tests/TestCase/Model/Table/HistoryTableTest.php 编写了
    /var/www/app/plugins/Data/tests/TestCase/Model/Table/HistoryTableTest.php
    完成

  • 通过
    $this->addPlugin('Data')加载新创建的插件

  • 通过加载的bin/cake插件验证插件是否已加载

  • 已确认存在
    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';
        }
    }
    
  • debug
    添加到
    CORE/src/CORE/App.php
    中,它针对
    \Model\Table\HistoryTable
    运行测试

    debug([
        '$plugin' => $plugin,
        '$name' => $name,
        '$base' => $base,
        '$fullname' => $fullname,
        '_classExistsInBase' => static::_classExistsInBase($fullname, $base),
    ]);
    

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

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

    对于您的本地
    数据
    插件,其外观如下:

    {
    // ...
    “自动加载”:{
    “psr-4”:{
    // ...
    “数据\\”:“插件/Data/src/”
    }
    },
    “自动加载开发”:{
    “psr-4”:{
    // ...
    “数据\\Test\\”:“插件/数据/测试/”
    }
    }
    }
    
    然后卸载自动加载器:

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

    另见


    遇到此类问题时,请不要使用虚构的名称,无论您多么确信自己没有犯错误,仍然可能会有错误,而这里的人只会浪费时间。对于该插件,您是否使用默认连接以外的连接?@ndm好的,没问题,我已经用从数据库中复制的真实数据编辑了这个问题console@Salines是的,它使用了不同的数据源,我粘贴了
    HistoryTable
    源,它有一个
    defaultConnectionName
    方法插件的名称空间是否正确映射到您的
    composer.json
    ,自动装弹机也相应地甩了?非常感谢你一如既往!
    debug([
        '$plugin' => $plugin,
        '$name' => $name,
        '$base' => $base,
        '$fullname' => $fullname,
        '_classExistsInBase' => static::_classExistsInBase($fullname, $base),
    ]);
    
    CORE/src/Core/App.php (line 70)
    [
    '$plugin' => 'Data',
    '$name' => 'History',
    '$base' => 'Data',
    '$fullname' => '\Model\Table\HistoryTable',
    '_classExistsInBase' => false,
    ]