mrssoft / yii2-db

Yii2 数据库工具

安装: 172

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 1

开放性问题: 0

类型:yii2-extension

dev-master 2019-12-18 09:43 UTC

This package is auto-updated.

Last update: 2024-09-18 20:24:52 UTC


README

批量插入到数据库。

PHP Github Total Downloads

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

php composer.phar require --prefer-dist mrssoft/yii2-db "*"

或将以下内容添加到您的 composer.json 文件的 require 部分中:

"mrssoft/yii2-db": "*"

使用方法

$batch = new DbBatch();

$batch->add([
    'field1' => $value1,
    'field2' => $value2,
]);

$batch->add([
    'field1' => $value1,
    'field2' => $value2,
], $key)

$batch->addUnique([
    'field1' => $value1,
    'field2' => $value2,
]);

$bool = $batch->insert('{{%table}}', true);
$bool = $batch->replace('{{%table}}');
$batch = new DbBatch([
    'maxItemsInQuery' => 1000,
    'table' => '{{%table}}',
    'truncate' => true,
    'command' => DbBatch::COMMAND_INSERT
]);

$batch->add([
    'field1' => $value1,
    'field2' => $value2,
], $key);

$batch->update([
    'field1' => $value3,
    'field2' => $value4,
], $key);

$count = $batch->getCount();
$keys = $batch->getKeys();
$element = $batch->get($key);
$data = $batch->getData();
$batch->setData($data);

$bool = $batch->execute();