|
15 | 15 | use CodeIgniter\Database\RawSql;
|
16 | 16 | use CodeIgniter\Test\CIUnitTestCase;
|
17 | 17 | use CodeIgniter\Test\DatabaseTestTrait;
|
| 18 | +use Config\Database; |
18 | 19 | use stdclass;
|
19 | 20 | use Tests\Support\Database\Seeds\CITestSeeder;
|
20 | 21 |
|
@@ -654,4 +655,79 @@ public function testUpsertWithTestModeAndGetCompiledUpsert()
|
654 | 655 |
|
655 | 656 | $this-> assertStringContainsString( '[email protected]', $sql);
|
656 | 657 | }
|
| 658 | + |
| 659 | + public function testUpsertBatchWithQuery() |
| 660 | + { |
| 661 | + $this->forge = Database::forge($this->DBGroup); |
| 662 | + |
| 663 | + $this->forge->dropTable('user2', true); |
| 664 | + |
| 665 | + $this->forge->addField([ |
| 666 | + 'id' => ['type' => 'INTEGER', 'constraint' => 3, 'auto_increment' => true], |
| 667 | + 'name' => ['type' => 'VARCHAR', 'constraint' => 80], |
| 668 | + 'email' => ['type' => 'VARCHAR', 'constraint' => 100], |
| 669 | + 'country' => ['type' => 'VARCHAR', 'constraint' => 40], |
| 670 | + 'created_at' => ['type' => 'DATETIME', 'null' => true], |
| 671 | + 'updated_at' => ['type' => 'DATETIME', 'null' => true], |
| 672 | + 'deleted_at' => ['type' => 'DATETIME', 'null' => true], |
| 673 | + 'last_loggin' => ['type' => 'DATETIME', 'null' => true], |
| 674 | + ])->addKey('id', true)->addUniqueKey('email')->addKey('country')->createTable('user2', true); |
| 675 | + |
| 676 | + $data = [ |
| 677 | + [ |
| 678 | + 'name' => 'Derek Jones user2', |
| 679 | + |
| 680 | + 'country' => 'France', |
| 681 | + ], |
| 682 | + [ |
| 683 | + 'name' => 'Ahmadinejad user2', |
| 684 | + |
| 685 | + 'country' => 'Greece', |
| 686 | + ], |
| 687 | + [ |
| 688 | + 'name' => 'Richard A Causey user2', |
| 689 | + |
| 690 | + 'country' => 'France', |
| 691 | + ], |
| 692 | + [ |
| 693 | + 'name' => 'Chris Martin user2', |
| 694 | + |
| 695 | + 'country' => 'Greece', |
| 696 | + ], |
| 697 | + [ |
| 698 | + 'name' => 'New User user2', |
| 699 | + |
| 700 | + 'country' => 'US', |
| 701 | + ], |
| 702 | + [ |
| 703 | + 'name' => 'New User2 user2', |
| 704 | + |
| 705 | + 'country' => 'US', |
| 706 | + ], |
| 707 | + ]; |
| 708 | + $this->db->table('user2')->insertBatch($data); |
| 709 | + |
| 710 | + $rawSql = new RawSql('CURRENT_TIMESTAMP'); |
| 711 | + |
| 712 | + $updateFields = ['updated_at' => $rawSql]; |
| 713 | + |
| 714 | + $subQuery = $this->db->table('user2')->select('email, name, country'); |
| 715 | + |
| 716 | + $this->db->table('user')->updateFields($updateFields, true)->onConstraint('email')->upsertBatch($subQuery); |
| 717 | + |
| 718 | + $this-> seeInDatabase( 'user', [ 'name' => 'Derek Jones user2', 'email' => '[email protected]']); |
| 719 | + $this-> seeInDatabase( 'user', [ 'name' => 'New User user2', 'email' => '[email protected]']); |
| 720 | + |
| 721 | + $result = $this->db->table('user')->get()->getResultArray(); |
| 722 | + |
| 723 | + foreach ($result as $row) { |
| 724 | + if ( $row[ 'email'] === '[email protected]' || $row[ 'email'] === '[email protected]') { |
| 725 | + $this->assertNull($row['updated_at']); |
| 726 | + } else { |
| 727 | + $this->assertNotNull($row['updated_at']); |
| 728 | + } |
| 729 | + } |
| 730 | + |
| 731 | + $this->forge->dropTable('user2', true); |
| 732 | + } |
657 | 733 | }
|
0 commit comments