Skip to content

Commit ddf26e5

Browse files
authored
Merge pull request #6216 from kenjis/fix-builder-insert-update-objects
fix: Builder insert()/update() does not accept an object
2 parents ce951b1 + 8a63886 commit ddf26e5

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

system/Database/BaseBuilder.php

Lines changed: 6 additions & 3 deletions
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);
@@ -2012,11 +2014,12 @@ public function getCompiledUpdate(bool $reset = true)
20122014
/**
20132015
* Compiles an update string and runs the query.
20142016
*
2015-
* @param mixed $where
2017+
* @param array|object|null $set
2018+
* @param array|RawSql|string|null $where
20162019
*
20172020
* @throws DatabaseException
20182021
*/
2019-
public function update(?array $set = null, $where = null, ?int $limit = null): bool
2022+
public function update($set = null, $where = null, ?int $limit = null): bool
20202023
{
20212024
if ($set !== null) {
20222025
$this->set($set);

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');

tests/system/Database/Builder/UpdateTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,35 @@ protected function setUp(): void
3434
$this->db = new MockConnection([]);
3535
}
3636

37-
public function testUpdate()
37+
public function testUpdateArray()
3838
{
3939
$builder = new BaseBuilder('jobs', $this->db);
4040

41-
$builder->testMode()->where('id', 1)->update(['name' => 'Programmer'], null, null);
41+
$data = ['name' => 'Programmer'];
42+
$builder->testMode()->where('id', 1)->update($data, null, null);
43+
44+
$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
45+
$expectedBinds = [
46+
'id' => [
47+
1,
48+
true,
49+
],
50+
'name' => [
51+
'Programmer',
52+
true,
53+
],
54+
];
55+
56+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledUpdate()));
57+
$this->assertSame($expectedBinds, $builder->getBinds());
58+
}
59+
60+
public function testUpdateObject()
61+
{
62+
$builder = new BaseBuilder('jobs', $this->db);
63+
64+
$data = (object) ['name' => 'Programmer'];
65+
$builder->testMode()->where('id', 1)->update($data, null, null);
4266

4367
$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
4468
$expectedBinds = [

user_guide_src/source/changelogs/v4.2.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BREAKING
1414

1515
- Now ``Services::request()`` returns ``IncomingRequest`` or ``CLIRequest``.
1616
- The method signature of ``CodeIgniter\Debug\Exceptions::__construct()`` has been changed. The ``IncomingRequest`` typehint on the ``$request`` parameter was removed. Extending classes should likewise remove the parameter so as not to break LSP.
17+
- The method signature of ``BaseBuilder.php::insert()`` and ``BaseBuilder.php::update()`` have been changed. The ``?array`` typehint on the ``$set`` parameter was removed.
1718

1819
Enhancements
1920
************

0 commit comments

Comments
 (0)