• 批量写入数据.insertAll
    • 函数原型
    • 用法如下:
    • 绑定参数
    • 使用 bind 绑定参数
    • replace 支持

    批量写入数据.insertAll

    函数原型

    插入成功后,返回 lastInsertId 。

    1. public function insertAll($data, $arrBind = [], $booReplace = false, $bFlag = false);

    用法如下:

    1. # INSERT INTO `test` (`test`.`name`,`test`.`value`) VALUES (:name,:value),(:name_1,:value_1),(:name_2,:value_2),(:name_3,:value_3)
    2. /*
    3. Array
    4. (
    5. [name] => Array
    6. (
    7. [0] => 小鸭子1
    8. [1] => 2
    9. )
    10. [value] => Array
    11. (
    12. [0] => 呱呱呱1
    13. [1] => 2
    14. )
    15. [name_1] => Array
    16. (
    17. [0] => 小鸭子2
    18. [1] => 2
    19. )
    20. [value_1] => Array
    21. (
    22. [0] => 呱呱呱2
    23. [1] => 2
    24. )
    25. [name_2] => Array
    26. (
    27. [0] => 小鸭子3
    28. [1] => 2
    29. )
    30. [value_2] => Array
    31. (
    32. [0] => 呱呱呱3
    33. [1] => 2
    34. )
    35. [name_3] => Array
    36. (
    37. [0] => 小鸭子4
    38. [1] => 2
    39. )
    40. [value_3] => Array
    41. (
    42. [0] => 呱呱呱4
    43. [1] => 2
    44. )
    45. )
    46. */
    47. $data = [
    48. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
    49. ['name' => '小鸭子2', 'value' => '呱呱呱2'],
    50. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
    51. ['name' => '小鸭子4', 'value' => '呱呱呱4'],
    52. ];
    53. Db::table('test')->
    54. insertAll($data);

    绑定参数

    1. # INSERT INTO `test` (`test`.`name`,`test`.`value`) VALUES (:name,:value),(:name_1,:questionmark_0_1),(:name_2,:value_2),(:name_3,:questionmark_1_3)
    2. /*
    3. Array
    4. (
    5. [name] => Array
    6. (
    7. [0] => 小鸭子1
    8. [1] => 2
    9. )
    10. [value] => Array
    11. (
    12. [0] => 呱呱呱1
    13. [1] => 2
    14. )
    15. [name_1] => Array
    16. (
    17. [0] => 小鸭子2
    18. [1] => 2
    19. )
    20. [questionmark_0_1] => Array
    21. (
    22. [0] => 吃肉1
    23. [1] => 2
    24. )
    25. [name_2] => Array
    26. (
    27. [0] => 小鸭子3
    28. [1] => 2
    29. )
    30. [value_2] => Array
    31. (
    32. [0] => 呱呱呱3
    33. [1] => 2
    34. )
    35. [name_3] => Array
    36. (
    37. [0] => 小鸭子4
    38. [1] => 2
    39. )
    40. [questionmark_1_3] => Array
    41. (
    42. [0] => 吃肉2
    43. [1] => 2
    44. )
    45. )
    46. */
    47. $data = [
    48. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
    49. ['name' => '小鸭子2', 'value' => '[?]'],
    50. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
    51. ['name' => '小鸭子4', 'value' => '[?]'],
    52. ];
    53. Db::table('test')->
    54. insertAll($data, ['吃肉1', '吃肉2']);
    55. # INSERT INTO `test` (`test`.`name`,`test`.`value`) VALUES (:name,:value),(:name_1,:hello),(:name_2,:value_2),(:name_3,:world)
    56. /*
    57. Array
    58. (
    59. [name] => Array
    60. (
    61. [0] => 小鸭子1
    62. [1] => 2
    63. )
    64. [value] => Array
    65. (
    66. [0] => 呱呱呱1
    67. [1] => 2
    68. )
    69. [name_1] => Array
    70. (
    71. [0] => 小鸭子2
    72. [1] => 2
    73. )
    74. [name_2] => Array
    75. (
    76. [0] => 小鸭子3
    77. [1] => 2
    78. )
    79. [value_2] => Array
    80. (
    81. [0] => 呱呱呱3
    82. [1] => 2
    83. )
    84. [name_3] => Array
    85. (
    86. [0] => 小鸭子4
    87. [1] => 2
    88. )
    89. [hello] => hello 吃肉
    90. [world] => world 喝汤
    91. )
    92. */
    93. $data = [
    94. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
    95. ['name' => '小鸭子2', 'value' => '[:hello]'],
    96. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
    97. ['name' => '小鸭子4', 'value' => '[:world]'],
    98. ];
    99. Db::table('test')->
    100. insertAll($data, ['hello' => 'hello 吃肉', 'world' => 'world 喝汤']);

    使用 bind 绑定参数

    1. # INSERT INTO `test` (`test`.`name`,`test`.`value`) VALUES (:name,:value),(:name_1,:questionmark_0_1),(:name_2,:value_2),(:name_3,:questionmark_1_3)
    2. /*
    3. Array
    4. (
    5. [name] => Array
    6. (
    7. [0] => 小鸭子1
    8. [1] => 2
    9. )
    10. [value] => Array
    11. (
    12. [0] => 呱呱呱1
    13. [1] => 2
    14. )
    15. [name_1] => Array
    16. (
    17. [0] => 小鸭子2
    18. [1] => 2
    19. )
    20. [questionmark_0_1] => Array
    21. (
    22. [0] => 吃鱼
    23. [1] => 2
    24. )
    25. [name_2] => Array
    26. (
    27. [0] => 小鸭子3
    28. [1] => 2
    29. )
    30. [value_2] => Array
    31. (
    32. [0] => 呱呱呱3
    33. [1] => 2
    34. )
    35. [name_3] => Array
    36. (
    37. [0] => 小鸭子4
    38. [1] => 2
    39. )
    40. [questionmark_1_3] => Array
    41. (
    42. [0] => 吃肉
    43. [1] => 2
    44. )
    45. )
    46. */
    47. $data = [
    48. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
    49. ['name' => '小鸭子2', 'value' => '[?]'],
    50. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
    51. ['name' => '小鸭子4', 'value' => '[?]'],
    52. ];
    53. Db::table('test')->
    54. bind(['吃鱼', '吃肉'])->
    55. insertAll($data);

    replace 支持

    1. # REPLACE INTO `test` (`test`.`name`,`test`.`value`) VALUES (:name,:value),(:name_1,:questionmark_0_1),(:name_2,:value_2),(:name_3,:questionmark_1_3)
    2. /*
    3. Array
    4. (
    5. [name] => Array
    6. (
    7. [0] => 小鸭子1
    8. [1] => 2
    9. )
    10. [value] => Array
    11. (
    12. [0] => 呱呱呱1
    13. [1] => 2
    14. )
    15. [name_1] => Array
    16. (
    17. [0] => 小鸭子2
    18. [1] => 2
    19. )
    20. [questionmark_0_1] => Array
    21. (
    22. [0] => 吃鱼
    23. [1] => 2
    24. )
    25. [name_2] => Array
    26. (
    27. [0] => 小鸭子3
    28. [1] => 2
    29. )
    30. [value_2] => Array
    31. (
    32. [0] => 呱呱呱3
    33. [1] => 2
    34. )
    35. [name_3] => Array
    36. (
    37. [0] => 小鸭子4
    38. [1] => 2
    39. )
    40. [questionmark_1_3] => Array
    41. (
    42. [0] => 吃肉
    43. [1] => 2
    44. )
    45. )
    46. */
    47. $data = [
    48. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
    49. ['name' => '小鸭子2', 'value' => '[?]'],
    50. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
    51. ['name' => '小鸭子4', 'value' => '[?]'],
    52. ];
    53. Db::table('test')->
    54. bind(['吃鱼', '吃肉'])->
    55. insertAll($data, [], true);