c24-toys/doctrine-bulk
Composer 安装命令:
composer require c24-toys/doctrine-bulk
包简介
Simple classes that allows bulk operations on doctrine entities (only mySQL upsert supported)
README 文档
README
Adds ability to multiple upsert / insert on duplicate (MySQL only) of entities or array to database using doctrine schema and ORM objects.
INSERT ... ON DUPLICATE KEY UPDATE Statement for ORM objects
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
Be warned: this function will just take your list of your ORM objects and will create the insert on duplicate sql query
- Supported Relationship / Join Types are ONE_TO_ONE AND MANY_TO_ONE
- LifeCycleCallbacks Events::prePersist / Events::preUpdate are supported
- Your ORM objects can be detached from session to avoid the insert/update triggered by ORM.. ** I opted to give you flexibility here, use $detach parameter
- Changes will be sent to DB in chunks
If the world would be nice and Doctrine able to do something else then single insert/update queries for MySQL, i would not need to do this.
Save the planet with less energy used for wasted queries :)
Samples
Default usage
<?php declare(strict_types = 1); use Doctrine\ORM\EntityManagerInterface; use DoctrineBulk\Bulk\BulkUpsert; /** * Class DbWrite */ class DbWrite { private EntityManagerInterface $manager; /** * Creates two users in one query. * * @return int */ public function updateExistingUsersAndCreateTwoUsers(): int { $dbUsers = []; // imagine some loaded users from DB and some changed data from your code $bulkUpsert = new BulkUpsert($this->manager, User::class); foreach ($dbUsers as $dbUser) { $bulkUpsert->addEntity($dbUser, detach: true); } // now 2 new users $bulkUpsert->addEntity(new User('user 1', 'password'), detach: true); $bulkUpsert->addEntity(new User('user 2', 'password'), detach: true); $firstInsertedId = (int) $bulkUpsert->execute(maxRows: 1000); return $firstInsertedId; } }
Forked from https://github.com/6dreams/doctrine-bulk
c24-toys/doctrine-bulk 适用场景与选型建议
c24-toys/doctrine-bulk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 992 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 c24-toys/doctrine-bulk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 c24-toys/doctrine-bulk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 992
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-24