krak / static-seed
强大的静态数据播种工具。
v0.2.1
2019-09-20 04:12 UTC
Requires
- doctrine/dbal: ^2.5
- krak/fn: ^1.1
- psr/log: ^1.0
Requires (Dev)
- peridot-php/peridot: ^1.19
This package is auto-updated.
Last update: 2024-09-20 15:40:31 UTC
README
Static Seed 提供了一种以可靠和可维护的方式管理查找表数据的方法。无需复杂迁移文件或自定义同步脚本,静态种子允许您在 JSON 中定义表信息,并将数据导入/导出到数据库中。
安装
使用 composer 在 krak/static-seed 下安装
使用方法
<?php use Krak\StaticSeed; const SEED_DIR = __DIR__ . '/path/to/seed/dir'; /** Setup the export tables */ function export(StaticSeed\Export $export) { $export->export([ new StaticSeed\Table('colors', 'tuple'), new StaticSeed\Table('color_sets', 'struct'), StaticSeed\Table::createJoin('color_sets_colors', 'color_set_id', [ 'color_set_id' => ['table' => 'color_sets', 'field' => 'slug'], 'color_id' => ['table' => 'colors', 'field' => 'slug'], ]) ], SEED_DIR); } function import(StaticSeed\Import $import) { $import->import(SEED_DIR); } function usage($argv) { printf("usage: %s <export|import>\n", $argv[0]); return 1; } function main($argv) { if (count($argv) <= 1) { exit(usage($argv)); } $conn = myDoctrineDbalConnection(); if ($argv[1] == 'export') { export(new StaticSeed\Export($conn)); } else if ($argv[1] == 'import') { import(new StaticSeed\Import($conn)); } else { exit(usage($argv)); } } main($argv);
导出将生成一组类似这样的文件
- colors.json
- colors_sets.json
- colors_sets_colors.json
colors.json
{
"name": "colors",
"row_type": "tuple",
"type": "normal",
"fields": ["id", "name", "slug", "hex", "sort", "type"],
"rows": [
[1, "Red", "red", "#ff0000", 0, "normal"],
[2, "Green", "green", "#00ff00", 0, "normal"],
[3, "Blue", "blue", "#0000ff", 0, "normal"],
]
}
color_sets.json
{
"name": "color_sets",
"row_type": "struct",
"type": "normal",
"fields": ["id", "name", "slug"],
"rows": [
{"id": 1, "name": "Color Set 1", "slug": "color-set-1"},
{"id": 1, "name": "Color Set 2", "slug": "color-set-2"}
]
}
color_sets_colors.json
{
"name": "color_sets_colors",
"type": "join",
"row_type": "tuple",
"index_by": "color_set_id",
"fields": [
"color_set_id",
"color_id"
],
"map_id": {
"color_set_id": {
"table": "color_sets",
"field": "slug"
},
"color_id": {
"table": "colors",
"field": "slug"
}
},
"rows": {
"color-set-1": [
"red",
"green",
],
"color-set-2": [
"green",
"blue"
]
}
}
Symfony 安装
在您的 config/bundles.php 中注册以下内容以安装 symfony 扩展包。
Krak\StaticSeed\Bridge\Symfony\StaticSeedBundle::class => ['all' => true],