插入单条数据.insert

Testing Is Documentation

tests/Database/Create/InsertTest.php插入单条数据.insert - 图1

Uses

  1. <?php
  2. use Leevel\Database\Condition;
  3. use Tests\Database\DatabaseTestCase as TestCase;

insert 基本用法

写入成功后,返回 lastInsertId

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdonamedparameter_value)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子"
  10. ],
  11. "pdonamedparameter_value": [
  12. "吃饭饭"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $data = ['name' => '小鸭子', 'value' => '吃饭饭'];
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->sql()
  24. ->table('test_query')
  25. ->insert($data)
  26. )
  27. );
  28. }

insert 绑定参数

  1. public function testBind(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdopositional2namedparameter_0)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子"
  10. ],
  11. "pdopositional2namedparameter_0": [
  12. "吃肉"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $data = ['name' => '小鸭子', 'value' => Condition::raw('?')];
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->sql()
  24. ->table('test_query')
  25. ->insert($data, ['吃肉'])
  26. )
  27. );
  28. $sql = <<<'eot'
  29. [
  30. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:value)",
  31. {
  32. "pdonamedparameter_name": [
  33. "小鸭子"
  34. ],
  35. "value": "呱呱呱"
  36. },
  37. false
  38. ]
  39. eot;
  40. $data = ['name' => '小鸭子', 'value' => Condition::raw(':value')];
  41. $this->assertSame(
  42. $sql,
  43. $this->varJson(
  44. $connect
  45. ->sql()
  46. ->table('test_query')
  47. ->insert($data, ['value' => '呱呱呱']),
  48. 1
  49. )
  50. );
  51. }

TIP

位置占位符会自动转为命名占位符,以增强灵活性。

bind.insert 绑定参数写入数据

  1. public function testWithBindFunction(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdopositional2namedparameter_0)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子"
  10. ],
  11. "pdopositional2namedparameter_0": [
  12. "吃鱼"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $data = ['name' => '小鸭子', 'value' => Condition::raw('?')];
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->sql()
  24. ->table('test_query')
  25. ->bind(['吃鱼'])
  26. ->insert($data)
  27. )
  28. );
  29. }

insert 支持 replace 用法

  1. public function testReplace(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "REPLACE INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:value)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子"
  10. ],
  11. "value": "呱呱呱"
  12. },
  13. false
  14. ]
  15. eot;
  16. $data = ['name' => '小鸭子', 'value' => Condition::raw(':value')];
  17. $this->assertSame(
  18. $sql,
  19. $this->varJson(
  20. $connect
  21. ->sql()
  22. ->table('test_query')
  23. ->insert($data, ['value' => '呱呱呱'], true)
  24. )
  25. );
  26. }

insert 支持字段指定表名

  1. public function testInsertSupportTable(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "REPLACE INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:value)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子"
  10. ],
  11. "value": "呱呱呱"
  12. },
  13. false
  14. ]
  15. eot;
  16. $data = ['name' => '小鸭子', 'test_query.value' => Condition::raw(':value')];
  17. $this->assertSame(
  18. $sql,
  19. $this->varJson(
  20. $connect
  21. ->sql()
  22. ->table('test_query')
  23. ->insert($data, ['value' => '呱呱呱'], true)
  24. )
  25. );
  26. }

insert 空数据写入示例

  1. public function testInsertWithEmptyData(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` () VALUES ()",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $data = [];
  12. $this->assertSame(
  13. $sql,
  14. $this->varJson(
  15. $connect
  16. ->sql()
  17. ->table('test_query')
  18. ->insert($data)
  19. )
  20. );
  21. }

insert.replace 空数据写入示例

  1. public function testReplaceWithEmptyData(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "REPLACE INTO `test_query` () VALUES ()",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $data = [];
  12. $this->assertSame(
  13. $sql,
  14. $this->varJson(
  15. $connect
  16. ->sql()
  17. ->table('test_query')
  18. ->insert($data, [], true)
  19. )
  20. );
  21. }