Skip to content

Commit 7314bed

Browse files
committed
refactor: complete backed enum bind support
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 723fa2f commit 7314bed

5 files changed

Lines changed: 44 additions & 4 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Database;
1515

16+
use BackedEnum;
1617
use Closure;
1718
use CodeIgniter\Database\Exceptions\DatabaseException;
1819
use CodeIgniter\Database\Exceptions\DataException;
@@ -3939,7 +3940,7 @@ protected function objectToArray($object)
39393940
$array = [];
39403941

39413942
foreach (get_object_vars($object) as $key => $val) {
3942-
if ((! is_object($val) || $val instanceof RawSql) && ! is_array($val)) {
3943+
if (($val instanceof BackedEnum || ! is_object($val) || $val instanceof RawSql) && ! is_array($val)) {
39433944
$array[$key] = $val;
39443945
}
39453946
}

system/Database/BasePreparedQuery.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Database;
1515

1616
use ArgumentCountError;
17+
use BackedEnum;
1718
use CodeIgniter\Database\Exceptions\DatabaseException;
1819
use CodeIgniter\Events\Events;
1920
use CodeIgniter\Exceptions\BadMethodCallException;
@@ -122,6 +123,12 @@ abstract public function _prepare(string $sql, array $options = []);
122123
*/
123124
public function execute(...$data)
124125
{
126+
foreach ($data as $key => $value) {
127+
if ($value instanceof BackedEnum) {
128+
$data[$key] = $value->value;
129+
}
130+
}
131+
125132
// Execute the Query.
126133
$startTime = microtime(true);
127134

tests/system/Database/Builder/InsertTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ public function testInsertWithBackedEnum(): void
8080
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
8181
}
8282

83+
public function testInsertObjectWithBackedEnum(): void
84+
{
85+
$builder = $this->db->table('jobs');
86+
87+
$builder->testMode()->insert((object) [
88+
'id' => 1,
89+
'status' => StatusEnum::ACTIVE,
90+
], true);
91+
92+
$expectedSQL = 'INSERT INTO "jobs" ("id", "status") VALUES (1, \'active\')';
93+
94+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
95+
}
96+
8397
public function testInsertObject(): void
8498
{
8599
$builder = $this->db->table('jobs');

tests/system/Database/Live/PreparedQueryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use CodeIgniter\Test\DatabaseTestTrait;
2424
use PHPUnit\Framework\Attributes\Group;
2525
use Tests\Support\Database\Seeds\CITestSeeder;
26+
use Tests\Support\Enum\StatusEnum;
2627

2728
/**
2829
* @internal
@@ -286,6 +287,19 @@ public function testExecuteSelectQueryAndCheckTypeAndResult(): void
286287
$this->assertSame($expectedRow, $result->getRowArray());
287288
}
288289

290+
public function testExecuteWithBackedEnum(): void
291+
{
292+
$this->query = $this->db->prepare(static fn ($db) => $db->table('team_members')
293+
->select('person_id')
294+
->where('status', StatusEnum::PENDING)
295+
->get());
296+
297+
$result = $this->query->execute(StatusEnum::ACTIVE);
298+
299+
$this->assertInstanceOf(ResultInterface::class, $result);
300+
$this->assertCount(2, $result->getResultArray());
301+
}
302+
289303
public function testExecuteSelectQueryManualAndCheckTypeAndResult(): void
290304
{
291305
$this->query = $this->db->prepare(static function ($db): Query {

user_guide_src/source/database/queries.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ automatically escaped producing safer queries.
193193
You don't have to remember to manually escape data - the engine does it automatically for you.
194194

195195
.. versionadded:: 4.8.0
196-
Query bindings and Query Builder bound values accept PHP ``BackedEnum``
197-
values. CodeIgniter uses the enum backing value when escaping the bound
198-
value.
196+
Query bindings, prepared query values, and Query Builder bound values accept
197+
PHP ``BackedEnum`` values. CodeIgniter uses the enum backing value before
198+
escaping or passing the value to the database driver.
199199

200200
.. literalinclude:: queries/032.php
201201

@@ -319,6 +319,10 @@ query:
319319

320320
.. literalinclude:: queries/019.php
321321

322+
.. versionadded:: 4.8.0
323+
Prepared query values accept PHP ``BackedEnum`` values and pass their
324+
backing values to the database driver.
325+
322326
For queries of type "write" it returns true or false, indicating the success or failure of the query.
323327
For queries of type "read" it returns a standard :doc:`result set </database/results>`.
324328

0 commit comments

Comments
 (0)