mrssoft / yii2-db
Yii2 数据库工具
dev-master
2019-12-18 09:43 UTC
Requires
- php: >=7.1
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-09-18 20:24:52 UTC
README
批量插入到数据库。
安装
安装此扩展的首选方式是通过 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();