Skip to content

Commit 11e6d1a

Browse files
committed
fix: RawSql disappear without error
Throws an exception.
1 parent 40e1f90 commit 11e6d1a

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,6 +3209,10 @@ protected function objectToArray($object)
32093209
return $object;
32103210
}
32113211

3212+
if ($object instanceof RawSql) {
3213+
throw new InvalidArgumentException('RawSql "' . $object . '" cannot be used here.');
3214+
}
3215+
32123216
$array = [];
32133217

32143218
foreach (get_object_vars($object) as $key => $val) {

tests/system/Database/Builder/InsertTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Database\RawSql;
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\Mock\MockConnection;
19+
use InvalidArgumentException;
1920

2021
/**
2122
* @internal
@@ -279,4 +280,22 @@ public function testInsertBatchThrowsExceptionOnEmptyData()
279280
$this->expectExceptionMessage('insertBatch() has no data.');
280281
$builder->insertBatch([]);
281282
}
283+
284+
public function testSetIncorrectRawSqlUsage()
285+
{
286+
$this->expectException(InvalidArgumentException::class);
287+
$this->expectExceptionMessage(
288+
'RawSql "expires = DATE_ADD(NOW(), INTERVAL 2 HOUR)" cannot be used here.'
289+
);
290+
291+
$builder = $this->db->table('auth_bearer');
292+
293+
$builder->testMode()
294+
->set([
295+
'jti' => 'jti',
296+
'proctorID' => '12',
297+
])
298+
->set(new RawSql('expires = DATE_ADD(NOW(), INTERVAL 2 HOUR)'))
299+
->insert();
300+
}
282301
}

0 commit comments

Comments
 (0)