Skip to content

Commit 7be2371

Browse files
authored
Merge pull request #7148 from kenjis/fix-Builder-set-RawSql
fix: [QueryBuilder] RawSql passed to set() disappears without error
2 parents 9a71a94 + 11e6d1a commit 7be2371

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
@@ -3215,6 +3215,10 @@ protected function objectToArray($object)
32153215
return $object;
32163216
}
32173217

3218+
if ($object instanceof RawSql) {
3219+
throw new InvalidArgumentException('RawSql "' . $object . '" cannot be used here.');
3220+
}
3221+
32183222
$array = [];
32193223

32203224
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)