|
14 | 14 | use CodeIgniter\Database\Exceptions\DatabaseException; |
15 | 15 | use CodeIgniter\Test\CIUnitTestCase; |
16 | 16 | use CodeIgniter\Test\DatabaseTestTrait; |
| 17 | +use Config\Database; |
17 | 18 | use Tests\Support\Database\Seeds\CITestSeeder; |
18 | 19 |
|
19 | 20 | /** |
@@ -90,4 +91,62 @@ public function testDeleteBatch() |
90 | 91 |
|
91 | 92 | $this->dontSeeInDatabase('user', ['email' => 'ahmadinejad@world.com', 'name' => 'Ahmadinejad']); |
92 | 93 | } |
| 94 | + |
| 95 | + public function testDeleteBatchWithQuery() |
| 96 | + { |
| 97 | + $this->forge = Database::forge($this->DBGroup); |
| 98 | + |
| 99 | + $this->forge->dropTable('user2', true); |
| 100 | + |
| 101 | + $this->forge->addField([ |
| 102 | + 'id' => ['type' => 'INTEGER', 'constraint' => 3, 'auto_increment' => true], |
| 103 | + 'name' => ['type' => 'VARCHAR', 'constraint' => 80], |
| 104 | + 'email' => ['type' => 'VARCHAR', 'constraint' => 100], |
| 105 | + 'country' => ['type' => 'VARCHAR', 'constraint' => 40], |
| 106 | + 'created_at' => ['type' => 'DATETIME', 'null' => true], |
| 107 | + 'updated_at' => ['type' => 'DATETIME', 'null' => true], |
| 108 | + 'deleted_at' => ['type' => 'DATETIME', 'null' => true], |
| 109 | + 'last_loggin' => ['type' => 'DATETIME', 'null' => true], |
| 110 | + ])->addKey('id', true)->addUniqueKey('email')->addKey('country')->createTable('user2', true); |
| 111 | + |
| 112 | + $data = [ |
| 113 | + [ |
| 114 | + 'name' => 'Derek Jones', |
| 115 | + 'email' => 'derek@world.com', |
| 116 | + 'country' => 'France', |
| 117 | + ], |
| 118 | + [ |
| 119 | + 'name' => 'Ahmadinejad does not match', |
| 120 | + 'email' => 'ahmadinejad@world.com', |
| 121 | + 'country' => 'Greece', |
| 122 | + ], |
| 123 | + [ |
| 124 | + 'name' => 'Chris Martin', |
| 125 | + 'email' => 'chris@world.com', |
| 126 | + 'country' => 'Greece', |
| 127 | + ], |
| 128 | + ]; |
| 129 | + $this->db->table('user2')->insertBatch($data); |
| 130 | + |
| 131 | + $query = $this->db->table('user2')->select('email, name, country')->where('country', 'Greece'); |
| 132 | + |
| 133 | + $builder = $this->db->table('user')->setQueryAsData($query, 'alias'); |
| 134 | + |
| 135 | + if ($this->db->DBDriver === 'SQLite3') { |
| 136 | + $builder->onConstraint('email, name'); |
| 137 | + } else { |
| 138 | + $builder->onConstraint('email'); |
| 139 | + $builder->where('alias.name = user.name'); |
| 140 | + } |
| 141 | + |
| 142 | + $builder->deleteBatch(); |
| 143 | + |
| 144 | + $this->seeInDatabase('user', ['name' => 'Derek Jones', 'email' => 'derek@world.com']); |
| 145 | + $this->seeInDatabase('user', ['name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com']); |
| 146 | + $this->dontSeeInDatabase('user', ['name' => 'Chris Martin', 'email' => 'chris@world.com']); |
| 147 | + |
| 148 | + $result = $this->db->table('user')->get()->getResultArray(); |
| 149 | + |
| 150 | + $this->forge->dropTable('user2', true); |
| 151 | + } |
93 | 152 | } |
0 commit comments