color-rabbit / doctrine-helper
代替 `php bin/console doctrine:mapping:import`
v0.1.3
2024-09-20 08:11 UTC
Requires
- php: >=8.2
- doctrine/dbal: ^3|^4
- symfony/framework-bundle: ^7.0
- symfony/http-client: ^7.0
Requires (Dev)
- symfony/console: ^7.0
- symfony/http-kernel: ^7.0
This package is auto-updated.
Last update: 2024-09-20 08:11:46 UTC
README
英文 | 中文
此工具提供了一种替代 Symfony 7 中 `php bin/console doctrine:mapping:import App\\Entity attribute --path=src/Entity` 命令的方法,因为 `doctrine:mapping:import` 命令在最新版本中已被移除。它允许将现有数据库中的实体映射导入到 Symfony 应用程序中。
支持的数据库
- MySQL
- PostgreSQL
- SQLServer
- Oracle
- Sqlite3
支持的字段类型
MySQL
- 不支持
bit
和enum
类型 - 转换相似类型(例如,
double
转换为float
)
PostgreSQL
- 支持数值、字符串、日期、货币、JSON、UUID、二进制和布尔类型
- 不支持复杂或自定义类型
SQLServer
- 支持精确数值、近似数值、日期/时间、字符串(char,varchar)、Unicode 字符串(nchar,nvarchar)、二进制字符串(binary,varbinary)和其他数据类型
- 不支持已弃用或复杂的数据类型
Oracle
- 支持各种数据类型,如 NUMBER、FLOAT、CHAR、VARCHAR2、DATE、TIMESTAMP、RAW、CLOB、BLOB 等。
- 不支持其他数据类型
Sqlite
- 支持整数、实数、浮点数、双精度浮点数、十进制、varchar、blob、text、日期、datetime 和布尔类型
安装
composer require color-rabbit/doctrine-helper --dev
同步数据库表到项目
php bin/console doctrine-helper:mapping:import attribute --path=Test --ucfrist=true --table=dict,log --without-table-prefix=eq_ php bin/console doctrine-helper:mapping:import --ucfirst=true
命令行选项
type
:数据库字段描述类型(attribute,xml,yaml,php)(默认:attribute
,目前仅支持attribute
)--path
:存储实体和仓库类文件的路径(默认:src/{Entity|Repository}/{$path}
)--ucfirst=true
:生成 Symfony 6 风格的实体(私有属性为 camelCase),以便无缝代码迁移(默认:Symfony 7 风格,私有属性为下划线命名)--table
:导入特定表以生成相应的实体和仓库类--without-table-prefix
:生成实体时忽略表前缀
在这篇文章中,我们将以 MySQL 为例来演示生成结果。
假设您的数据库包含一个名为 "test" 的表,其中包含几乎所有的 MySQL 数据类型,并具有唯一索引、组合索引和常规索引
create table test ( id int not null, int_1 int default 1 not null comment 'int', int_2 smallint default 2 null comment 'smallint', int_3 int default 3 null comment 'tinyint', int_4 mediumint default 4 null comment 'mediumint', int_5 bigint not null comment 'bigint' primary key, int_6 double default 6 null comment 'float', int_7 double default 7 null comment 'double', int_8 decimal default 8 null comment 'decimal', date_1 date default (curdate()) null comment 'date', date_2 time default (curtime()) null comment 'time', date_3 datetime default (now()) null comment 'datetime', date_4 timestamp default (now()) null comment 'timestamp', date_5 year default (now()) null comment 'year', str_1 char default 'a' null comment 'char', str_2 varchar(255) default 'b' null comment 'varchar(255)', str_3 varbinary(1) default 0x63 null comment 'binary', str_4 varbinary(1) default 0x64 null comment 'varbinary(1)', str_8 set ('a', 'b', 'c') default 'a,b' null comment 'set', json_1 json null comment 'json', bool_1 int default 0 null comment 'bool', constraint I_int_2 unique (int_2) comment '唯一索引' ) comment '测试表'; create index I_int_1 on test (int_1) comment '普通索引'; create index I_int_3 on test (int_3, int_4, int_5) comment '联合索引'; create index I_int_4 on test (int_6);
使用以下命令生成相应的实体和仓库类文件
php bin/console doctrine-helper:mapping:import attribute --path=Test --ucfrist=true --table=dict,log --without-table-prefix=eq_ php bin/console doctrine-helper:mapping:import --ucfirst=true --table=test
新创建的 Test 实体类看起来像这样
// src/Entity/Test.php namespace App\Entity; use App\Repository\TestRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Table(name: 'test')] #[ORM\UniqueConstraint(name: 'I_int_2', columns: ['int_2'])] #[ORM\Index(name: 'I_int_1', columns: ['int_1'])] #[ORM\Index(name: 'I_int_3', columns: ['int_3', 'int_4', 'int_5'])] #[ORM\Index(name: 'I_int_4', columns: ['int_6'])] #[ORM\Entity(repositoryClass: TestRepository::class)] class Test { #[ORM\Column(name: "int_5", type: Types::BIGINT, options: ["comment" => "bigint"])] #[ORM\Id] #[ORM\GeneratedValue(strategy: "NONE")] private ?string $int5 = null; #[ORM\Column(name: "id")] private ?int $id = null; #[ORM\Column(name: "int_1", options: ["comment" => "int", "default" => 1])] private ?int $int1 = 1; #[ORM\Column(name: "int_2", type: Types::SMALLINT, nullable: true, options: ["comment" => "smallint", "default" => 2])] private ?int $int2 = 2; #[ORM\Column(name: "int_3", nullable: true, options: ["comment" => "tinyint", "default" => 3])] private ?int $int3 = 3; #[ORM\Column(name: "int_4", nullable: true, options: ["comment" => "mediumint", "default" => 4])] private ?int $int4 = 4; #[ORM\Column(name: "int_6", nullable: true, options: ["comment" => "float", "default" => 6])] private ?float $int6 = 6; #[ORM\Column(name: "int_7", nullable: true, options: ["comment" => "double", "default" => 7])] private ?float $int7 = 7; #[ORM\Column(name: "int_8", type: Types::DECIMAL, precision: 10, scale: 0, nullable: true, options: ["comment" => "decimal", "default" => 8])] private ?string $int8 = '8'; #[ORM\Column(name: "date_1", type: Types::DATE_MUTABLE, nullable: true, options: ["comment" => "date", "default" => 'curdate()'])] private ?\DateTimeInterface $date1 = null; #[ORM\Column(name: "date_2", type: Types::TIME_MUTABLE, nullable: true, options: ["comment" => "time", "default" => 'curtime()'])] private ?\DateTimeInterface $date2 = null; #[ORM\Column(name: "date_3", type: Types::DATETIME_MUTABLE, nullable: true, options: ["comment" => "datetime", "default" => 'now()'])] private ?\DateTimeInterface $date3 = null; #[ORM\Column(name: "date_4", type: Types::DATETIME_MUTABLE, nullable: true, options: ["comment" => "timestamp", "default" => 'now()'])] private ?\DateTimeInterface $date4 = null; #[ORM\Column(name: "date_5", type: Types::DATETIME_MUTABLE, nullable: true, options: ["comment" => "year", "default" => 'now()'])] private ?\DateTimeInterface $date5 = null; #[ORM\Column(name: "str_1", length: 1, nullable: true, options: ["comment" => "char", "fixed" => true, "default" => 'a'])] private ?string $str1 = 'a'; #[ORM\Column(name: "str_2", length: 255, nullable: true, options: ["comment" => "varchar(255)", "default" => 'b'])] private ?string $str2 = 'b'; #[ORM\Column(name: "str_3", type: Types::BINARY, length: 1, nullable: true, options: ["comment" => "binary", "default" => '0x63'])] private $str3 = 0x63; #[ORM\Column(name: "str_4", type: Types::BINARY, length: 1, nullable: true, options: ["comment" => "varbinary(1)", "default" => '0x64'])] private $str4 = 0x64; #[ORM\Column(name: "str_8", type: Types::SIMPLE_ARRAY, nullable: true, options: ["comment" => "set", "default" => 'a,b'])] private ?array $str8 = ["a","b"]; #[ORM\Column(name: "json_1", nullable: true, options: ["comment" => "json"])] private ?array $json1 = null; #[ORM\Column(name: "bool_1", nullable: true, options: ["comment" => "bool", "default" => 0])] private ?int $bool1 = 0; // ...getter and setter }
请随意探索和使用此工具来管理您的 Symfony 应用程序中的 Doctrine 映射!