Skip to content

Commit 29ed7b4

Browse files
committed
fix: Builder insert() does not accept an object
1 parent 7439b6e commit 29ed7b4

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,11 +1878,13 @@ public function getCompiledInsert(bool $reset = true)
18781878
/**
18791879
* Compiles an insert string and runs the query
18801880
*
1881+
* @param array|object|null $set
1882+
*
18811883
* @throws DatabaseException
18821884
*
18831885
* @return bool|Query
18841886
*/
1885-
public function insert(?array $set = null, ?bool $escape = null)
1887+
public function insert($set = null, ?bool $escape = null)
18861888
{
18871889
if ($set !== null) {
18881890
$this->set($set, '', $escape);

tests/system/Database/Builder/InsertTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function setUp(): void
3333
$this->db = new MockConnection([]);
3434
}
3535

36-
public function testSimpleInsert()
36+
public function testInsertArray()
3737
{
3838
$builder = $this->db->table('jobs');
3939

@@ -59,6 +59,32 @@ public function testSimpleInsert()
5959
$this->assertSame($expectedBinds, $builder->getBinds());
6060
}
6161

62+
public function testInsertObject()
63+
{
64+
$builder = $this->db->table('jobs');
65+
66+
$insertData = (object) [
67+
'id' => 1,
68+
'name' => 'Grocery Sales',
69+
];
70+
$builder->testMode()->insert($insertData, true);
71+
72+
$expectedSQL = 'INSERT INTO "jobs" ("id", "name") VALUES (1, \'Grocery Sales\')';
73+
$expectedBinds = [
74+
'id' => [
75+
1,
76+
true,
77+
],
78+
'name' => [
79+
'Grocery Sales',
80+
true,
81+
],
82+
];
83+
84+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
85+
$this->assertSame($expectedBinds, $builder->getBinds());
86+
}
87+
6288
public function testThrowsExceptionOnNoValuesSet()
6389
{
6490
$builder = $this->db->table('jobs');

0 commit comments

Comments
 (0)