赵伟胜/mysql-workbench-schema-exporter

MySQL Workbench Schema Exporter

v1.0.6 2016-09-02 12:04 UTC

This package is auto-updated.

Last update: 2024-09-15 01:00:28 UTC


README

这是一个从 https://github.com/johmue/mysql-workbench-schema-exporter 分支出来的项目,为了改进并继续其开发,因为它看起来在原始仓库中已被遗弃了::(

什么是 MySQL Workbench schema exporter?

该应用程序旨在创建

MySQL Workbench模型 (*.mwb) 的 schema 文件。它受到 mysql-workbench-doctrine-plugin 的启发。

Doctrine 行为

为 Doctrine1 实现了对行为的支持。使用表中的注释字段。

{d:actAs}
  actAs:
    timestampable:
      [..]
{/d:actAs}

外键名称

要将关系名称替换为外键名称,请以外键名称开头为 "d:"。

命令行界面 (CLI)

有一个新的 CLI 用于简化导出过程,名为 export.php,您可以在 cli 文件夹下查看。CLI 具有在导出之前自定义导出配置的功能。默认情况下,CLI 应用程序将使用当前目录中位于的配置文件 export.json 来提供参数,如果找到它。要禁用此行为,请参阅下面的选项。

CLI 的语法

php cli/export.php [options] FILE [DEST]

其中

  • 选项:
    • --export=type,选择导出的结果,目前可用的类型
      • doctrine1-yaml,Doctrine 1.0 YAML schema
      • doctrine2-yaml,Doctrine 2.0 YAML schema
      • doctrine2-annotation,Doctrine 2.0 Annotation classes(默认)
      • propel1-xml,Propel XML schema
      • zend-dbtable,Zend DbTable
      • zend-rest-controller,Zend Rest Controller
      • sencha-extjs3,Sencha ExtJS3 Model
    • --config=file,从文件(JSON 格式)读取导出参数
    • --saveconfig,将导出参数保存到文件 export.json,以后可以用于 --config=file 的值
    • --list-exporter,显示所有可用的导出器
    • --no-auto-config,禁用自动配置文件查找
    • --zip,压缩结果
    • --help,显示用法(或抑制任何参数)
  • FILE,要导出的 mwb 文件
  • DEST,目标目录(可选),如果未指定,则假定当前目录

示例用法

php cli/export.php --export=doctrine2-annotation example/data/test.mwb example/result

php cli/export.php --config=example/data/config.json example/data/test2.mwb example/result

doctrine2-annotation 的示例导出参数(JSON)

{
    "export": "doctrine2-annotation",
    "zip": false,
    "dir": "temp",
    "params": {
        "backupExistingFile": true,
        "skipPluralNameChecking": false,
        "enhanceManyToManyDetection": true,
        "bundleNamespace": "",
        "entityNamespace": "",
        "repositoryNamespace": "",
        "useAnnotationPrefix": "ORM\\",
        "useAutomaticRepository": false,
        "indentation": 4,
        "filename": "%entity%.%extension%",
        "quoteIdentifier": false,
        "propertyNameCodingStyle": "lowercamelcase",
        "tableNameCodingStyle": "underscore",
        "columnNameCodingStyle": "underscore"
    }
}

Coding style options:
* raw
* underscore
* uppercamelcase
* lowercamelcase

导出器选项

常规选项

应用于所有格式化程序的常规选项。

  • 缩进

    生成代码的缩进大小。

  • useTabs

    使用制表符进行缩进而不是空格。设置此选项将忽略 indentation 选项

  • filename

    输出文件名格式,使用以下标记 %schema%%table%%entity%%extension% 允许文件名替换为上下文数据。默认为 %entity%.%extension%

  • skipPluralNameChecking

    跳过检查模型的复数名称,保留原样,对于非英语表名很有用。默认为false

  • backupExistingFile

    如果目标已存在,则在替换内容之前创建备份。默认为true

  • enhanceManyToManyDetection

    如果启用,将在生成的代码中添加表之间的多对多关系。默认为true

Doctrine 1.0选项列表

  • extendTableNameWithSchemaName

    在表名旁边包含模式名称。默认为false

  • {d:externalRelations}

Doctrine 2.0 YAML选项列表

  • useAutomaticRepository

    自动生成实体仓库类名称。

  • bundleNamespace

    实体类名称的全局命名空间前缀。

  • entityNamespace

    实体命名空间。默认为Entity

  • tablePrefix 默认为

    为表名定义一个前缀。默认为

  • repositoryNamespace

    实体仓库类名称的命名空间前缀。要应用此配置,必须将useAutomaticRepository设置为true

  • extendTableNameWithSchemaName

    在表名旁边包含模式名称。默认为false

Doctrine 2.0 注释选项列表

  • useAnnotationPrefix

    Doctrine 注释前缀。默认为ORM\

  • useAutomaticRepository

    见上文。

  • bundleNamespace

    见上文。

  • entityNamespace

    见上文。

  • tablePrefix 默认为

    见上文。

  • repositoryNamespace

    见上文。

  • skipGetterAndSetter

    不生成列的getter和setter。默认为false

  • generateEntitySerialization

    生成方法__sleep(),当实体序列化时仅包括实际列。默认为true

  • quoteIdentifier

    如果启用此选项,所有表名和列名都将使用反引号(`)进行引用。当表名或列名包含保留词时很有用。默认为false

  • {d:m2m}false{/d:m2m}

    MySQL Workbench模式导出器尝试自动猜测哪些表是多对多映射表,并且不会为这些表生成实体类。如果一个表包含恰好两个指向不同表的外键,并且这些表不是多对多映射表,则该表被认为是映射表。

    有时这种猜测对你来说是不正确的。但是你可以在表的注释中添加提示,表明它不是映射表。只需在表的注释中任何位置使用"{d:m2m}false{/d:m2m}"即可。

  • {d:unidirectional}true{/d:unidirectional}

    默认情况下,所有外键都会导致双向关系。如果你想只有一个单向关系,请将标志添加到外键的注释中。

  • {d:owningSide}true{/d:owningSide}

    在双向多对多映射表中,关系的拥有方是随机选择的。如果你将此提示添加到m2m-table的一个外键中,你可以为Doctrine定义拥有方。

  • {d:cascade}persist, merge, remove, detach, all{/d:cascade}

    你可以在外键的注释中指定Doctrine级联选项。它们将被生成到注释中。(参考

  • {d:fetch}EAGER{/d:fetch}

    你可以在外键的注释中指定关系的获取类型。(EAGER或LAZY,Doctrine默认为LAZY)

  • {d:orphanRemoval}true{/d:orphanRemoval}

    你可以在外键的注释中设置的另一个选项。(参考

Propel Xml选项列表

  • namespace
  • addVendor

Zend DbTable选项列表

  • tablePrefix
  • parentTable
  • generateDRI
  • generateGetterSetter

Zend Rest Controller选项列表

  • tablePrefix
  • parentTable

Sencha ExtJS3 Model选项列表

  • classPrefix
  • parentClass

需求

与PHP 5.3及以上版本兼容。

链接

测试数据库

示例

<?php

// enable autoloading of classes
$libDir = __DIR__.'/lib';
require_once($libDir.'/MwbExporter/SplClassLoader.php');

$classLoader = new SplClassLoader();
$classLoader->setIncludePath($libDir);
$classLoader->register();

// create bootstrap
$bootstrap = new \MwbExporter\Bootstrap();

// define a formatter and do configuration
$formatter = $bootstrap->getFormatter('doctrine2-annotation');
$formatter->setup(array());

// specify the workbench document to load, output directory, and storage type (zip or file)
$mwbfile = 'myDatabaseModel.mwb';
$outDir = getcwd();
$storage = 'zip';
// load document and export
$document = $bootstrap->export($formatter, $mwbfile, $outDir, $storage);

// show the output
echo sprintf("Saved to %s.\n\n", $document->getWriter()->getStorage()->getResult());

?>