定制 yadakhov/insert-on-duplicate-key 二次开发

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

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

yadakhov/insert-on-duplicate-key

最新稳定版本:v1.3.0

Composer 安装命令:

composer require yadakhov/insert-on-duplicate-key

包简介

A trait for MySQL INSERT ON DUPLICATE KEY UPDATE.

README 文档

README

Latest Stable Version License Build Status

Insert Duplicate Key Update is a quick way to do mass insert.

It's a trait meant to be used with Laravel's Eloquent ORM.

Code Example

use Illuminate\Database\Eloquent\Model;
use Yadakhov\InsertOnDuplicateKey;

/**
 * Class User.
 */
class User extends Model
{
    // The function is implemented as a trait.
    use InsertOnDuplicateKey;
}

Multi values insert.

    $users = [
        ['id' => 1, 'email' => 'user1@email.com', 'name' => 'User One'],
        ['id' => 2, 'email' => 'user2@email.com', 'name' => 'User Two'],
        ['id' => 3, 'email' => 'user3@email.com', 'name' => 'User Three'],
    ];

Important: the order of the keys are important. It should be the same for every arrays. The reason is the code uses array_values().

Do not do this:

    $users = [
        ['id' => 1, 'email' => 'user1@email.com', 'name' => 'User One'],
        ['email' => 'user2@email.com', 'id' => 2, 'name' => 'User Two'],
        ['email' => 'user3@email.com', 'name' => 'User Three', 'id' => 3],
    ];

INSERT ON DUPLICATE KEY UPDATE

    User::insertOnDuplicateKey($users);
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three')
    ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)
    User::insertOnDuplicateKey($users, ['email']);
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three')
    ON DUPLICATE KEY UPDATE `email` = VALUES(`email`)

If users have a numeric column we would like, for example, to sum:

    $users = [
        ['id' => 1, 'name' => 'User One', 'heritage' => 1000],
        ['id' => 2, 'name' => 'User Two', 'heritage' => 2000],
        ['id' => 3, 'name' => 'User Three', 'heritage' => 1500],
    ];
    User::insertOnDuplicateKey($users, ['heritage' => DB::raw('`heritage` + VALUES(`heritage`)')]);
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three')
    ON DUPLICATE KEY UPDATE `heritage` = `heritage` + VALUES(`heritage`)

INSERT IGNORE

    User::insertIgnore($users);
    -- produces this query
    INSERT IGNORE INTO `users`(`id`,`email`,`name`) VALUES
    (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three');

REPLACE INTO

    User::replace($users);
    -- produces this query
    REPLACE INTO `users`(`id`,`email`,`name`) VALUES
    (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three');

created_at and updated_at fields.

created_at and updated_at will not be updated automatically. To update you can pass the fields in the insert array.

['id' => 1, 'email' => 'user1@email.com', 'name' => 'User One', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]

Run unit tests

./vendor/bin/phpunit

Will this work on Postgresql?

No. On Duplicate Key Update is only available on MySQL. Postgresql 9.4 has a similar feature called UPSERT. Implementing UPSERT is left as an exercise for the reader.

Isn't this the same as updateOrCreate()?

It is similar but not the same. The updateOrCreate() will only work for one row at a time which doesn't allow bulk insert. InsertOnDuplicateKey will work on many rows.

统计信息

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

GitHub 信息

  • Stars: 276
  • Watchers: 12
  • Forks: 51
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-13