vi-kon/laravel-db-exporter

此包已被弃用且不再维护。未建议替换包。

数据库表结构及数据导出至迁移和种子文件

v1.0 2015-03-22 14:18 UTC

This package is not auto-updated.

Last update: 2021-03-05 22:45:02 UTC


README

这是为 Laravel 5 定制的数据库表结构和数据导出至迁移和种子文件的工具

目录

特性

  • 从数据库表结构创建 迁移 文件
  • 处理外键(注意递归外键)
  • 从数据库表结构创建 模型 文件(包括外键)
  • 组织 生成的模型,根据数据库表名使用正则表达式到 单独的命名空间目录结构
  • 从数据库表内容创建 种子 文件

回到顶部

安装

基础

composer.json 文件中添加以下行

// to "require" object
"vi-kon/laravel-db-exporter": "~1.*"

或者,在项目根目录运行以下命令

composer require vi-kon/laravel-db-exporter

在 Laravel 5 项目中,将以下行添加到 app.php

// to providers array
'ViKon\DbExporter\DbExporterServiceProvider',

配置和迁移

安装配置和迁移文件,只需简单运行

php artisan vendor:publish --provider="ViKon\DbExporter\DbExporterServiceProvider"

回到顶部

配置

配置文件帮助设置命令的默认值。

return [
    /*
    | --------------------------------------------------------------------------
    | Default connection name
    | --------------------------------------------------------------------------
    | Database connection name. Valid connection names are defined in
    | database.php. If value is null default connection is used.
    |
    */
    'connection' => null,
    /*
    | --------------------------------------------------------------------------
    | Default table prefix
    | --------------------------------------------------------------------------
    | This table prefix is used in generated migration files or in generated
    | models.
    |
    */
    'prefix'     => '',
    /*
    | --------------------------------------------------------------------------
    | Default selected tables
    | --------------------------------------------------------------------------
    | Select specified database tables only. No work with ignore option
    | together. In commands with --select option can add another tables.
    |
    */
    'select'     => [],
    /*
    | --------------------------------------------------------------------------
    | Default ignored tables
    | --------------------------------------------------------------------------
    | Ignore specified database tables. No work with select option together.
    | In commands with --ignore option can add another tables.
    |
    */
    'ignore'     => [
        'migrations',
    ],
    /*
    | --------------------------------------------------------------------------
    | Model options
    | --------------------------------------------------------------------------
    */
    'model'      => [
        /*
        | --------------------------------------------------------------------------
        | Default namespace
        | --------------------------------------------------------------------------
        | Namespace for generated models. In command with --namespace option can
        | overwrite.
        |
        */
        'namespace' => 'App\Models',
        /*
        | --------------------------------------------------------------------------
        | Default path
        | --------------------------------------------------------------------------
        | Generated models destination path. Is relative to projects base path. In
        | command with --path option can overwrite.
        |
        */
        'path'      => app_path('Models'),
        /*
        | --------------------------------------------------------------------------
        | Custom map
        | --------------------------------------------------------------------------
        | Map is useful for organizing generated models to multiple namespaces and
        | directories. Each map entry is array with following associative keys:
        |
        | * tablePattern - regex pattern for selecting tables by name
        | * namespace    - generated models namespace from selected tables
        | * path         - generated models destination path from selected tables
        | * className    - array containing pattern and replacement for preg_match
        |                  to generate models class name from table name. If value
        |                  is null original table name is used. The result is camel
        |                  cased. The subject table name is snake cased
        |
        */
        'map'       => [
//            [
//                'tablePattern' => '.*',
//                'namespace'    => 'App\Models',
//                'path'         => 'app/Models',
//                'className'    => [
//                    'pattern'     => '',
//                    'replacement' => '',
//                ],
//            ],
        ],
    ],
    /*
    | --------------------------------------------------------------------------
    | Migration options
    | --------------------------------------------------------------------------
    */
    'migration'  => [
        /*
        | --------------------------------------------------------------------------
        | Default path
        | --------------------------------------------------------------------------
        | Generated migration destination path. Is relative to projects base path.
        | In command with --path option can overwrite.
        |
        */
        'path' => base_path('database/migrations'),
    ],
    /*
    | --------------------------------------------------------------------------
    | Seed options
    | --------------------------------------------------------------------------
    */
    'seed'       => [
        /*
        | --------------------------------------------------------------------------
        | Default path
        | --------------------------------------------------------------------------
        | Generated seed destination path. Is relative to projects base path.
        | In command with --path option can overwrite.
        |
        */
        'path' => base_path('database/seeds'),
    ],
];

回到顶部

用法

注意:生成的文件可能需要一些自动格式化。

创建迁移文件

使用 db-exporter:migrate 命令从数据库创建迁移文件。它有几个选项

  • prefix - 迁移文件中的数据库名称前缀
  • select - 选定的数据库表名数组(如果设置了 ignore 选项,则忽略)
  • ignore - 忽略的数据库表名数组
  • database - 指定数据库连接名称(如果未设置选项,则使用默认连接)
  • force - 强制覆盖现有迁移文件
  • path - 相对于项目根目录的输出目标路径(默认为 {PROJECT ROOT}/database/migrations

注意:由 migration.path 配置键提供的模型路径必须可由 PHP 编写以生成模型。

以下示例假设以下数据库表

  • users
  • groups
  • pages 与用户 id 的外键

导出默认数据库中的所有表

php artisan db-exporter:migrate

以上命令将在 {PROJECT ROOT}/database/migrations 目录中生成以下文件

YYYY-MM-DD_000000_create_users_table.php
YYYY-MM-DD_000001_create_groups_table.php
YYYY-MM-DD_000002_create_pages_table.php

注意:表名和列名被转换为蛇形小写。

回到顶部

创建模型

使用 db-exporter:models 命令从数据库创建模型。它有几个选项

  • prefix - 迁移文件中的数据库名称前缀
  • select - 选定的数据库表名数组(如果设置了 ignore 选项,则忽略)
  • ignore - 忽略的数据库表名数组
  • connection - 指定数据库连接名称(如果未设置选项,则使用默认连接)
  • force - 强制覆盖现有迁移文件
  • namespace - 模型命名空间(默认为 App\Models
  • path - 相对于项目根目录的输出目标路径(默认为 {PROJECT ROOT}/database/migrations

注意:在某些情况下,外部方法名可能与模型匹配,因此需要进行手动重命名。

注意:在某些情况下,关系猜测(一对一多对一一对多)可能会在单个类中生成相同的方法名。

注意:由配置键 model.path 提供的模型路径必须可以被 PHP 写入,以便生成模型。

从默认数据库创建模型

php artisan db-exporter:models

创建种子文件

使用 db-exporter:seed 命令可以从数据库数据中创建种子。它有几个选项

  • prefix - 迁移文件中的数据库名称前缀
  • select - 选定的数据库表名数组(如果设置了 ignore 选项,则忽略)
  • ignore - 忽略的数据库表名数组
  • connection - 指定数据库连接名称(如果未设置选项,则使用默认连接)
  • force - 强制覆盖现有迁移文件
  • 路径 - 相对于项目根目录的输出目标路径(默认为 database/seeds

注意:由配置键 seed.path 提供的种子路径必须可以被 PHP 写入,以便生成种子类。

从默认数据库创建种子文件

php artisan db-exporter:seed

回到顶部

许可证

此软件包根据 MIT 许可证授权

回到顶部