kfirba / import-query-generator
一个高效的批量资源导入查询生成器,可以区分新记录和需要更新的记录。
v1.0
2018-01-12 10:38 UTC
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2024-09-08 15:01:23 UTC
README
这是一个高效的批量资源导入查询生成器,可以区分新记录和需要更新的记录。该库使用了MySQL的ON DUPLICATE KEY UPDATE
功能。
前言
我强烈推荐您至少浏览一下关于这个库的博客,以更好地了解这个库。
安装
您可以使用Composer将此库添加为本地、按项目依赖项到您的项目中。
composer require kfirba/import-query-generator
使用
use Kfirba\QueryGenerator; $table = 'users'; $data = [ ['name' => 'John', 'email' => 'john@example.com', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')], ['name' => 'Jane', 'email' => 'jane@example.com', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')], ['name' => 'Susy', 'email' => 'susy@example.com', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')], ]; $excludedColumnsFromUpdate = ['password', 'created_at']; $queryObject = (new QueryGenerator)->generate($table, $data, $excludedColumnsFromUpdate); $queryObject->getQuery(); // -> "insert into `users` (`name`,`email`,`password`,`created_at`,`updated_at`) values (?,?,?,?,?),(?,?,?,?,?),(?,?,?,?,?) on duplicate key update `name`=VALUES(`name`),`email`=VALUES(`email`),`updated_at`=VALUES(`updated_at`)" $queryObject->getBindings(); // -> ['John', 'john@example.com', 'hashed_password', '2018-01-12', '2018-01-12', 'Jane', 'jane@example.com', 'hashed_password', '2018-01-12', '2018-01-12', 'Susy', 'Susy@example.com', 'hashed_password', '2018-01-12', '2018-01-12']
如您所注意到的,生成器默认使用column=VALUES(column)
,因为这通常是我们在尝试批量导入数据时使用的。需要其他行为?您可以提交一个PR,或者打开一个问题,我们可以讨论它 🤓。
许可证
本软件包是开源软件,许可协议为MIT许可证。