定制 c24-toys/doctrine-bulk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

c24-toys/doctrine-bulk

最新稳定版本:4.0.2

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

统计信息

  • 总下载量: 992
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固