icedevelopment/mysql-workbench-schema-exporter

MySQL Workbench Schema Exporter

v1.0.5 2014-07-22 07:10 UTC

This package is auto-updated.

Last update: 2024-09-20 02:00:37 UTC


README

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

什么是MySQL Workbench模式导出器?

该应用程序旨在创建

  • Doctrine 1.0 YAML Schema
  • Doctrine 2.0 YAML Schema 和 注解类
  • Zend DbTable
  • Zend Rest Controller
  • Sencha ExtJS3 Model
  • Propel XML Schema
  • CakePHP (未实现)

从MySQL Workbench模型 (*.mwb) 创建模式文件。它受到 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 注解类(默认值)
      • 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 默认值为empty

    定义表名称的前缀。默认值为empty

  • repositoryNamespace

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

  • extendTableNameWithSchemaName

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

Doctrine 2.0 注释选项列表

  • useAnnotationPrefix

    Doctrine 注释前缀。默认值为ORM\

  • useAutomaticRepository

    见上方。

  • bundleNamespace

    见上方。

  • entityNamespace

    见上方。

  • tablePrefix 默认值为empty

    见上方。

  • 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表的某个外键,则可以定义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());

?>