|
13 | 13 |
|
14 | 14 | use CodeIgniter\Test\CIUnitTestCase;
|
15 | 15 | use CodeIgniter\Test\DatabaseTestTrait;
|
| 16 | +use Config\Database; |
16 | 17 | use Tests\Support\Database\Seeds\CITestSeeder;
|
17 | 18 |
|
18 | 19 | /**
|
@@ -116,4 +117,68 @@ public function testInsertPasswordHash()
|
116 | 117 |
|
117 | 118 | $this->seeInDatabase('misc', ['value' => $hash]);
|
118 | 119 | }
|
| 120 | + |
| 121 | + public function testInsertWithQuery() |
| 122 | + { |
| 123 | + $this->forge = Database::forge($this->DBGroup); |
| 124 | + |
| 125 | + $this->forge->dropTable('user2', true); |
| 126 | + |
| 127 | + $this->forge->addField([ |
| 128 | + 'id' => ['type' => 'INTEGER', 'constraint' => 3, 'auto_increment' => true], |
| 129 | + 'name' => ['type' => 'VARCHAR', 'constraint' => 80], |
| 130 | + 'email' => ['type' => 'VARCHAR', 'constraint' => 100], |
| 131 | + 'country' => ['type' => 'VARCHAR', 'constraint' => 40], |
| 132 | + 'created_at' => ['type' => 'DATETIME', 'null' => true], |
| 133 | + 'updated_at' => ['type' => 'DATETIME', 'null' => true], |
| 134 | + 'deleted_at' => ['type' => 'DATETIME', 'null' => true], |
| 135 | + 'last_loggin' => ['type' => 'DATETIME', 'null' => true], |
| 136 | + ])->addKey('id', true)->addUniqueKey('email')->addKey('country')->createTable('user2', true); |
| 137 | + |
| 138 | + $data = [ |
| 139 | + [ |
| 140 | + 'name' => 'Derek Jones user2', |
| 141 | + |
| 142 | + 'country' => 'France', |
| 143 | + ], |
| 144 | + [ |
| 145 | + 'name' => 'Ahmadinejad user2', |
| 146 | + |
| 147 | + 'country' => 'Greece', |
| 148 | + ], |
| 149 | + [ |
| 150 | + 'name' => 'Richard A Causey user2', |
| 151 | + |
| 152 | + 'country' => 'France', |
| 153 | + ], |
| 154 | + [ |
| 155 | + 'name' => 'Chris Martin user2', |
| 156 | + |
| 157 | + 'country' => 'Greece', |
| 158 | + ], |
| 159 | + [ |
| 160 | + 'name' => 'New User user2', |
| 161 | + |
| 162 | + 'country' => 'US', |
| 163 | + ], |
| 164 | + [ |
| 165 | + 'name' => 'New User2 user2', |
| 166 | + |
| 167 | + 'country' => 'US', |
| 168 | + ], |
| 169 | + ]; |
| 170 | + $this->db->table('user2')->insertBatch($data); |
| 171 | + |
| 172 | + $subQuery = $this->db->table('user2') |
| 173 | + ->select('user2.name, user2.email, user2.country') |
| 174 | + ->join('user', 'user.email = user2.email', 'left') |
| 175 | + ->where('user.email IS NULL'); |
| 176 | + |
| 177 | + $this->db->table('user')->insertBatch($subQuery); |
| 178 | + |
| 179 | + $this->seeInDatabase('user', ['name' => 'New User user2']); |
| 180 | + $this->seeInDatabase('user', ['name' => 'New User2 user2']); |
| 181 | + |
| 182 | + $this->forge->dropTable('user2', true); |
| 183 | + } |
119 | 184 | }
|
0 commit comments