|
16 | 16 | use CodeIgniter\Database\Exceptions\DatabaseException; |
17 | 17 | use CodeIgniter\Database\Exceptions\DataException; |
18 | 18 | use CodeIgniter\Entity\Entity; |
| 19 | +use Config\Database; |
19 | 20 | use InvalidArgumentException; |
20 | 21 | use stdClass; |
| 22 | +use Tests\Support\Entity\UUID; |
21 | 23 | use Tests\Support\Models\EventModel; |
22 | 24 | use Tests\Support\Models\JobModel; |
23 | 25 | use Tests\Support\Models\SecondaryModel; |
24 | 26 | use Tests\Support\Models\UserModel; |
| 27 | +use Tests\Support\Models\UUIDPkeyModel; |
25 | 28 | use Tests\Support\Models\ValidModel; |
26 | 29 | use Tests\Support\Models\WithoutAutoIncrementModel; |
27 | 30 |
|
@@ -377,6 +380,98 @@ public function testUpdateWithEntityNoAllowedFields(): void |
377 | 380 | $this->model->update($id, $entity); |
378 | 381 | } |
379 | 382 |
|
| 383 | + public function testUpdateEntityWithPrimaryKeyCast(): void |
| 384 | + { |
| 385 | + if ( |
| 386 | + $this->db->DBDriver === 'OCI8' |
| 387 | + || $this->db->DBDriver === 'Postgre' |
| 388 | + || $this->db->DBDriver === 'SQLSRV' |
| 389 | + || $this->db->DBDriver === 'SQLite3' |
| 390 | + ) { |
| 391 | + $this->markTestSkipped($this->db->DBDriver . ' does not work with binary data as string data.'); |
| 392 | + } |
| 393 | + |
| 394 | + $this->createUuidTable(); |
| 395 | + |
| 396 | + $this->createModel(UUIDPkeyModel::class); |
| 397 | + |
| 398 | + $entity = new UUID(); |
| 399 | + $entity->id = '550e8400-e29b-41d4-a716-446655440000'; |
| 400 | + $entity->value = 'test1'; |
| 401 | + |
| 402 | + $id = $this->model->insert($entity); |
| 403 | + $entity = $this->model->find($id); |
| 404 | + |
| 405 | + $entity->value = 'id'; |
| 406 | + $result = $this->model->save($entity); |
| 407 | + |
| 408 | + $this->assertTrue($result); |
| 409 | + |
| 410 | + $entity = $this->model->find($id); |
| 411 | + |
| 412 | + $this->assertSame('id', $entity->value); |
| 413 | + } |
| 414 | + |
| 415 | + public function testUpdateBatchEntityWithPrimaryKeyCast(): void |
| 416 | + { |
| 417 | + if ( |
| 418 | + $this->db->DBDriver === 'OCI8' |
| 419 | + || $this->db->DBDriver === 'Postgre' |
| 420 | + || $this->db->DBDriver === 'SQLSRV' |
| 421 | + || $this->db->DBDriver === 'SQLite3' |
| 422 | + ) { |
| 423 | + $this->markTestSkipped($this->db->DBDriver . ' does not work with binary data as string data.'); |
| 424 | + } |
| 425 | + |
| 426 | + // See https://github.com/codeigniter4/CodeIgniter4/pull/8282#issuecomment-1836974182 |
| 427 | + $this->markTestSkipped( |
| 428 | + 'batchUpdate() is currently not working due to data type issues in the generated SQL statement.' |
| 429 | + ); |
| 430 | + |
| 431 | + $this->createUuidTable(); |
| 432 | + |
| 433 | + $this->createModel(UUIDPkeyModel::class); |
| 434 | + |
| 435 | + $entity1 = new UUID(); |
| 436 | + $entity1->id = '550e8400-e29b-41d4-a716-446655440000'; |
| 437 | + $entity1->value = 'test1'; |
| 438 | + $id1 = $this->model->insert($entity1); |
| 439 | + |
| 440 | + $entity2 = new UUID(); |
| 441 | + $entity2->id = 'bd59cff1-7a24-dde5-ac10-7b929db6da8c'; |
| 442 | + $entity2->value = 'test2'; |
| 443 | + $id2 = $this->model->insert($entity2); |
| 444 | + |
| 445 | + $entity1 = $this->model->find($id1); |
| 446 | + $entity2 = $this->model->find($id2); |
| 447 | + |
| 448 | + $entity1->value = 'update1'; |
| 449 | + $entity2->value = 'update2'; |
| 450 | + |
| 451 | + $data = [ |
| 452 | + $entity1, |
| 453 | + $entity2, |
| 454 | + ]; |
| 455 | + $this->model->updateBatch($data, 'id'); |
| 456 | + |
| 457 | + $this->seeInDatabase('uuid', [ |
| 458 | + 'value' => 'update1', |
| 459 | + ]); |
| 460 | + $this->seeInDatabase('uuid', [ |
| 461 | + 'value' => 'update2', |
| 462 | + ]); |
| 463 | + } |
| 464 | + |
| 465 | + private function createUuidTable(): void |
| 466 | + { |
| 467 | + $forge = Database::forge($this->DBGroup); |
| 468 | + $forge->dropTable('uuid', true); |
| 469 | + $forge->addField([ |
| 470 | + 'id' => ['type' => 'BINARY', 'constraint' => 16], |
| 471 | + 'value' => ['type' => 'VARCHAR', 'constraint' => 400, 'null' => true], |
| 472 | + ])->addKey('id', true)->createTable('uuid', true); |
| 473 | + } |
| 474 | + |
380 | 475 | public function testUseAutoIncrementSetToFalseUpdate(): void |
381 | 476 | { |
382 | 477 | $key = 'key'; |
|
0 commit comments