schereersoftware/cakephp-schema

此包已被废弃且不再维护。作者建议使用raul338/cakephp-schema包。

CakePHP 3.0 的 Schema 保存和加载到文件

v1.1 2017-10-20 13:33 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:50:30 UTC


README

对于当前版本,请尝试原始的https://github.com/Laykou/cakephp-schema或其后续版本:https://github.com/raul338/cakephp-schema

CakePHP 3.0 的 Schema 插件

将 schema 保存到一个文件中,然后从 schema 文件恢复数据库。执行 cake migrations migrate 时会自动保存 schema。

支持的数据库

  • Postgres
  • MySQL
  • SQL Server
  • SQLite 尚不支持

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require scherersoftware/cakephp-schema

更新您的 `config/bootstrap.php``

Plugin::load('Schema', ['bootstrap' => true]);

使用方法

该插件将 default 连接的 schema 保存到 config/schema.php 文件中。结构类似于 fixtures 字段。

cake schema save

要加载 schema,执行以下操作

cake schema load

在加载之前将删除数据库中所有现有的表。您将被询问

Loading the schema from the file Database is not empty. 37 tables will be deleted.
Do you want to continue? (y/n)

要禁用此询问(并回答 yes),请运行

cake schema load --no-interaction

种子

Schema 插件允许您从 config/seed.php 文件中种子数据。`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 的类型映射。例如,如果您使用的是 JsonType 示例,seed 命令将自动将数组转换为 JSON。

您可以使用 schema generateseed 命令根据您的数据库内容自动生成 seed.php 文件。

使用 schema seedseed.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

Migrations 插件 / phinx 的种子

此插件扩展了由 cakephp/migrations 插件提供的 seed bake 任务,但自动包含数据库中的种子数据。

示例用法

bin/cake bake migration_seed Users --records

这将向src/config/Seeds/UsersSeed.php中写入新文件,包括当前DB的users表中所有记录。

待办事项

  • 在执行cake migrations migrate后自动创建schema.php文件
  • 数据播种
  • 测试
  • 更多选项和配置
  • 重构和清理代码

已知问题

  • SQLite不完全支持