mnapoli/dbal-schema
Composer 安装命令:
composer require mnapoli/dbal-schema
包简介
DB schema manager for Doctrine DBAL
README 文档
README
A schema manager for Doctrine DBAL: all the convenience of the Doctrine ORM, without using the ORM.
Why?
Doctrine ORM can automatically manage your DB schema based on your entity mapping. This feature is lost when using the DBAL instead of the ORM.
This package lets you achieve something similar by defining your DB schema with PHP code. It also lets you manage your database using a Symfony Console command similar to Symfony's native doctrine:schema:update command, as well as DB migrations.
Installation
composer require mnapoli/dbal-schema
Usage
1. Define a schema
Define your DB schema by implementing the SchemaDefinition interface:
class MySchemaDefinition implements \DbalSchema\SchemaDefinition { public function define(Schema $schema) { $usersTable = $schema->createTable('users'); $usersTable->addColumn('id', 'integer'); $usersTable->addColumn('email', 'string'); $usersTable->addColumn('lastLogin', 'datetime'); $usersTable->addColumn('score', 'float', [ 'notnull' => false, ]); $usersTable->setPrimaryKey(['id']); $usersTable->addUniqueIndex(['email']); } }
You can read the whole API available on Doctrine's documentation.
2. Set up the schema
Doctrine can now generate/update your database based on your schema.
Using Symfony
Here is an example of configuration that can go in your config/services.yml:
services: DbalSchema\SchemaDefinition: # Replace this with your class name alias: App\Database\MySchemaDefinition DbalSchema\DbalSchemaProvider: # Register the commands: DbalSchema\DbalSchemaCommand: DbalSchema\Command\UpdateCommand: DbalSchema\Command\PurgeCommand:
This configuration assumes your services are autowired.
Once the services are registered, you can now run the commands:
bin/console dbal:schema:update bin/console dbal:schema:purge
Using Silly
Using Silly you can ignore the many separate command classes and simply use the DbalSchemaCommand class:
$schema = new MySchemaDefinition(); $dbalConnection = /* your DBAL connection, see http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html */ $command = new DbalSchemaCommand($dbalConnection, $schema); $application = new Silly\Application(); $application->command('db [--force]', [$command, 'update']); $application->command('db-purge [--force]', [$command, 'purge']); $application->run();
If you are using the Silly PHP-DI edition it's even simpler as PHP-DI can instantiate the DbalSchemaCommand service:
$application->command('db [--force]', [DbalSchemaCommand::class, 'update']); $application->command('db-purge [--force]', [DbalSchemaCommand::class, 'purge']);
3. Optional: database migrations
If you prefer using database migrations instead of running bin/console dbal:schema:update, DBAL Schema integrates with Doctrine Migrations.
To set it up, we need the setService() method call to happen like in the example below:
use Doctrine\Migrations\DependencyFactory; use Doctrine\Migrations\Provider\SchemaProvider; use DbalSchema\DbalSchemaProvider; $doctrineMigrationDependencyFactory = DependencyFactory::fromConnection(...); $doctrineMigrationDependencyFactory->setService(SchemaProvider::class, new DbalSchemaProvider(new MySchemaDefinition()));
In Symfony, it can be done by installing the DoctrineMigrationsBundle and editing config/packages/doctrine_migrations.yaml:
doctrine_migrations: # ... services: Doctrine\Migrations\Provider\SchemaProvider: DbalSchema\DbalSchemaProvider
Now, you can run:
bin/console doctrine:migrations:diff
to generate migrations based on your DBAL schema.
mnapoli/dbal-schema 适用场景与选型建议
mnapoli/dbal-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.95k 次下载、GitHub Stars 达 97, 最近一次更新时间为 2017 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dbal」 「db」 「schema」 「doctrine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mnapoli/dbal-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mnapoli/dbal-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mnapoli/dbal-schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Extension for Opis JSON Schema
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Build forms from schema
统计信息
- 总下载量: 32.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 97
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-24