raul338 / cakephp-schema
为 CakePHP 4 保存和加载模式到文件
Requires
- php: >=7.2
- cakephp/bake: ~2.0
- cakephp/cakephp: ~4.0
- cakephp/migrations: ^3.0
- riimu/kit-phpencoder: 2.*
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.2
- dereuromark/cakephp-ide-helper: ^1.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^0.12.8
- phpstan/phpstan-phpunit: ^0.12.18
- phpunit/phpunit: ^8.5 || ^9.5
README
对于 CakePHP 3.x 的使用,请参阅 2.x 分支
将模式保存到一个文件中,并用作 Fixtures 的自动模式。在执行 cake migrations migrate
时会自动保存模式。这也允许使用测试套件数据进行本地测试,以便调试。
支持的资源
- Postgres
- MySQL
- SQLite
SQL Server尚未测试
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require raul338/cakephp-schema
更新您的 Application
类
public function bootstrapCli() { // .... $this->addOptionalPlugin('Schema'); }
使用方法
插件将 default
连接的模式保存到 config/schema.php
文件中。结构与 fixtures 字段相似。
cake schema save
要重新加载模式,这对于使用测试数据调试很有用。运行
cake schema load
播种
Schema 插件允许您从 config/seed.php
文件中播种数据。这也用于保存不相关的表(如来自 acl 插件的表或 sphinxlog
表)seed.php
文件应返回表和行的数组
<?php
// You can work with custom libraries here or use the Cake's ORM
return [
'articles' => [
[
'id' => 1,
'category_id' => 1,
'label' => 'CakePHP'
],
[
'id' => 2,
'label' => 'Schema plugin',
'json_type_field' => [
'i' => 'will convert',
'to' => 'json'
]
]
],
'categories' => [
[
'id' => 2,
'label' => 'Frameworks'
]
]
];
Seed 命令支持 CakePHP ORM 的类型映射。例如,如果您正在使用 cookbooks 中的 JsonType 示例,seed 命令将自动将数组转换为 JSON。
您可以使用 schema generateseed
命令根据您的数据库内容自动生成 seed.php 文件。
使用 schema seed
将 seed.php
的内容导入到您的数据库中。
Seed 命令将接受以下选项
connection
要使用的数据库连接。seed
要生成的种子文件的路径(默认为 "config/seed.php")path
schema.php 文件的路径(默认为 "config/schema.php")
其他示例
cake schema save --connection test
cake schema save --path config/schema/schema.lock
cake schema load --connection test --path config/schema/schema.lock --no-interaction
仅删除数据库中的所有表
cake schema drop
cake schema drop --connection test
播种示例
cake schema seed --truncate
cake schema generateseed --seed config/my_seed.php
如果您的表中使用 Tree Behavior,则可以从种子数据中恢复树:(它将重新计算 lft
& rght
值)
cake recover_tree categories
固定模型生成
此插件允许您通过使用 SchemaFixture
将生成的模式和种子用作固定模型和数据。您可以像书中所示一样扩展您的固定模型。
示例用法
bin/cake bake fixture --theme Schema Users
<?php declare(strict_types=1); namespace App\Test\Fixture; use Schema\TestSuite\Fixture\SchemaFixture; class UsersFixture extends SchemaFixture { // This class reads schema from users key in config/schema.php // and reads records from users key in config/seed.php if exists }
待办事项
- 在
cake migrations migrate
后自动创建 schema.php 文件 - 数据播种
- 测试
- 更多选项和配置
- 重构和清理代码