加载烘焙插件模型失败并显示 "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"
我将模型烘焙到插件中,但无法加载。
我做了什么:
烘焙模型
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
通过$this->addPlugin('Data');
加载了新创建的插件
已验证插件已通过 bin/cake plugin loaded
加载
确认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';
}
}
Added the plugin model 到任意控制器方法:
- 或者
$this->loadModel('Data.History');
,
- 或
TableRegistry::getTableLocator()->get('Data.History');
得到错误:
Table class for alias Data.History could not be found.
Cake\ORM\Exception\MissingTableClassException
CORE/src/ORM/Locator/TableLocator.php:245
我尝试了什么:
我查看了 CORE/src/ORM/Locator/TableLocator.php
并且我可以记录 _getClassName
receives Data.History
,但是 returns null
.我还记录了它循环通过的 $this->locations
,但插件路径不存在:
Array
(
[0] => Model/Table
)
将 debug
添加到 CORE/src/Core/App.php
right before 它针对 \Model\Table\HistoryTable
针对 _classExistsInBase
.
运行测试
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,
]
您知道问题是什么以及如何解决吗?我正在使用 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
,如果您允许,它会相应地更新它并转储自动加载器。
另见
我将模型烘焙到插件中,但无法加载。
我做了什么:
烘焙模型
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
通过
加载了新创建的插件$this->addPlugin('Data');
已验证插件已通过
加载bin/cake plugin loaded
确认
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'; } }
Added the plugin model 到任意控制器方法:
- 或者
$this->loadModel('Data.History');
, - 或
TableRegistry::getTableLocator()->get('Data.History');
- 或者
得到错误:
Table class for alias Data.History could not be found. Cake\ORM\Exception\MissingTableClassException CORE/src/ORM/Locator/TableLocator.php:245
我尝试了什么:
我查看了
CORE/src/ORM/Locator/TableLocator.php
并且我可以记录_getClassName
receivesData.History
,但是 returnsnull
.我还记录了它循环通过的$this->locations
,但插件路径不存在:Array ( [0] => Model/Table )
将
运行测试debug
添加到CORE/src/Core/App.php
right before 它针对\Model\Table\HistoryTable
针对_classExistsInBase
.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, ]
您知道问题是什么以及如何解决吗?我正在使用 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
,如果您允许,它会相应地更新它并转储自动加载器。
另见