dodo-it/entity-generator
Composer 安装命令:
composer require dodo-it/entity-generator
包简介
Entity generator from database for almost any DBAL
README 文档
README
Highly customizable (typed) entity generator from database. It can generate entities for whole database, table/view and from raw SQL query
Installation
$ composer require dodo-it/entity-generator
How to run:
$config = new \DodoIt\EntityGenerator\Generator\Config([ 'path' => __DIR__ . '/Entities', 'extends' => \Examples\Pdo\Entities\Entity::class, 'namespace' => 'Examples\Pdo\Entities' ]); $pdo = new \PDO('mysql:dbname=example;host=127.0.0.1', 'root', ''); $generatorFactory = new \DodoIt\EntityGenerator\Factory\GeneratorPdoFactory($pdo); $generator = $generatorFactory->create($config); $generator->generate();
What kind of entities can I get?
Tool is highly customizable and can generate various different entity types of which most interesting are:
- PHP 7.4 typed properties
class ArticleEntity extends YourBaseEntity { public int $id; public ?string $title; public bool $published; public ?\DateTimeInterface $created_at; }
- properties with phpdoc
class ArticleEntity extends YourBaseEntity { /** @var int */ protected $id; /** @var string */ protected $title; /** @var bool */ protected $published; /** @var \DateTimeInterface */ protected $created_at; }
- properties with getters and setters (methods body is customizable)
class ArticleEntity extends YourBaseEntity { public function getId(): int { return $this->id; } public function setId(int $value): self { $this['id'] = $value; return $this; } public function getTitle(): ?string { return $this->title; } public function setTitle(?string $value): self { $this['title'] = $value; return $this; } public function getPublished(): bool { return $this->published; } public function setPublished(bool $value): self { $this['published'] = $value; return $this; } public function getCreatedAt(): ?\DateTimeInterface { return $this->created_at; } public function setCreatedAt(?\DateTimeInterface $value): self { $this['created_at'] = $value; return $this; }
- phpdoc properties
/** * @property int $id * @property string $title * @property int $published * @property \DateTimeInterface $created_at */ class ArticleEntity extends YourBaseEntity { }
- properties with phpdoc
class ArticleEntity extends YourBaseEntity { /** @var int */ public $id; /** @var string */ public $title; /** @var bool */ public $published; /** @var \DateTimeInterface */ public $created_at;
- it can generate column constants (use generateColumnConstants option)
class ArticleEntity extends YourBaseEntity { public const TABLE_NAME = 'articles'; public const ID = 'id'; public const TITLE = 'title'; public const PUBLISHED = 'published'; public const CREATED_AT = 'created_at'; . . .
- almost any combination you can imagine, check src/Generator/Config.php for list of all options see example folder
You can add your own methods to entities and change getter/setter functions, they won't be overriden when regenerated if rewrite flag is set to false
Configuration
see src/Generator/Config.php
Docker integration
Entity Generator image
https://hub.docker.com/r/pifou25/entity-generator
Generation from existing database
The database should be accessible from the docker network, the hostname is either
the name of the Mysql running container, or localhost if db is running on host.
The below command will generate entities into ./entities directory.
docker run --rm -v $PWD/entities:/app/entities --network some-network \
-e MYSQL_HOSTNAME=some-mariadb \
-e MYSQL_DATABASE=exmple-database \
-e MYSQL_USERNAME=example-user \
-e MYSQL_PASSWORD=my_cool_secret \
pifou25/entity-generator
namespace and extends example
The base class must exist into /entities to be declared
docker run --rm -v $PWD/include/entities:/app/entities --network scripts_default \
-e MYSQL_HOSTNAME=myhost \
-e MYSQL_DATABASE=mydb \
-e MYSQL_USERNAME=myuser \
-e MYSQL_PASSWORD=mypwd \
-e ENTITY_NAMESPACE=Example\\Pdo\\Entities \
-e BASECLASS=Example\\Pdo\\Entities\Entity \
entity-generator
Generation from flat plain SQL file
The docker compose file create a new database and initialize it with SQL data,
you have to put SQL init file into ./examples directory. When db is ready,
the entity-generator start on it to generate PHP entities.
It is as simple as running this command from the docker directory :
docker compose up
dodo-it/entity-generator 适用场景与选型建议
dodo-it/entity-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 136.98k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「generator」 「mysql」 「pdo」 「nette」 「entity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dodo-it/entity-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dodo-it/entity-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dodo-it/entity-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
统计信息
- 总下载量: 136.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-23