pharako/mysql-dbal
Composer 安装命令:
composer require pharako/mysql-dbal
包简介
MySQL extensions for Doctrine DBAL
README 文档
README
MySQL DBAL
MySQL extensions for Doctrine DBAL.
Pharako\DBAL\Connection is an extension of Doctrine\DBAL\Connection—all functionality you get from the latter is also contained in the former, with a few add-ons specific to databases compatible with MySQL:
- multiple inserts
- single and multiple upserts (update records if they exist, insert them otherwise)
Supported databases
- MySQL
- MariaDB
Requirements
PHP 8.0 and above. See the releases page for previous versions that still work with PHP < 8.0.
Installation
Install via Composer:
$ composer require pharako/mysql-dbal
Usage
Instantiation and configuration
Most PHP frameworks will have some sort of service injection functionality to help you with configuration, but nothing stops you from doing it by hand.
Manually
use Doctrine\Common\EventManager; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Driver\PDOMySql\Driver; use Pharako\DBAL\Connection; $params = [ 'dbname' => 'my_db', 'host' => 'localhost', 'user' => 'username', 'password' => '***', 'driver' => 'pdo_mysql' ]; $dbal = new Connection( $params, new Driver(), new Configuration(), new EventManager() );
Symfony 2 and above
Just specify the DBAL connection class under wrapper_class in config.yml. All other configurations should remain the same:
doctrine: dbal: dbname: %database_name% host: %database_host% port: %database_port% user: %database_user% password: %database_password% driver: pdo_mysql wrapper_class: 'Pharako\DBAL\Connection'
You can read Doctrine DBAL Configuration for more information on wrapper_class and other options.
Extra functionality
Pharako's additional methods follow the structure of Doctrine's data retrieval and manipulation functionality, including binding types.
Multiple inserts
You can insert multiple records with one call—this will hit the database only once:
$data = [ [ 'name' => 'Foo', 'family_name' => 'Bar' ], [ 'name' => 'Fuzz', 'family_name' => 'Bazz' ] ]; $dbal->insert('my_table', $data);
Or, if you want to specify the types of the data to be inserted:
$dbal->insert('my_table', $data, [\PDO::PARAM_STR, \PDO::PARAM_STR]);
Single and multiple upserts (update if present, insert if new)
Before using this functionality, make sure you read Careful with those upserts below.
Building on the previous example and assuming the name field is a unique key in the table structure, the first two records will have their family_name fields updated to Rab and Zabb, respectively, and the last one will be inserted:
$data = [ [ 'name' => 'Foo', 'family_name' => 'Rab' ], [ 'name' => 'Fuzz', 'family_name' => 'Zabb' ], [ 'name' => 'New', 'family_name' => 'Foo' ] ]; $dbal->upsert('my_table', $data);
Again, this will hit the database only once.
If you want your upsert to update only a few columns and leave all the others untouched, you can pass it an array specifying those columns:
$data = [ 'who' => 'Them', 'where' => 'There', 'when' => 'Sometime', 'why' => 'Because' ]; $dbal->upsert( 'another_table', $data, [\PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_STR], ['where', 'when'] );
In this example, if the upsert results in an update, only the where and when fields will be updated. If the upsert results in an insert, all fields will be included.
Careful with those upserts
By and large, it is safe to execute upserts against tables of varied structures—those containing a single unique index, a multi-column unique index or even multiple unique indexes.
However, because upserts in MySQL are more involved than simple inserts and updates, you should not expect those methods to behave similarly in 100% of the cases (for example, LAST_INSERT_ID() in the context of an upsert may behave slightly differently than in that of an insert).
That's why the official documentation says that "In general, you should try to avoid using an ON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes" and "[...] an INSERT ... ON DUPLICATE KEY UPDATE statement against a table having more than one unique or primary key is also marked as unsafe."
Despite that, upserts will work just as expected but in edge case scenarios. If you want to play it extra safe, though, try to tighten your tests and make sure you get the expected results when the upsert updates your records as well as when it inserts them.
Development
If you want to test this package from your workstation, checkout the development environment.
Code contributions and bug reports are welcome. For pull requests, please use the development branch.
pharako/mysql-dbal 适用场景与选型建议
pharako/mysql-dbal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.73k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2016 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dbal」 「mysql」 「doctrine」 「multiple」 「insert」 「upsert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pharako/mysql-dbal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pharako/mysql-dbal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pharako/mysql-dbal 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Make pimcore migration simple
Use a REST API as if it was your local database
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
Use FileMaker through CWP as your backend database
统计信息
- 总下载量: 16.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-28