Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Builder insert()/update() does not accept an object #6216

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1878,11 +1878,13 @@ public function getCompiledInsert(bool $reset = true)
/**
* Compiles an insert string and runs the query
*
* @param array|object|null $set
*
* @throws DatabaseException
*
* @return bool|Query
*/
public function insert(?array $set = null, ?bool $escape = null)
public function insert($set = null, ?bool $escape = null)
{
if ($set !== null) {
$this->set($set, '', $escape);
Expand Down Expand Up @@ -2012,11 +2014,12 @@ public function getCompiledUpdate(bool $reset = true)
/**
* Compiles an update string and runs the query.
*
* @param mixed $where
* @param array|object|null $set
* @param array|RawSql|string|null $where
*
* @throws DatabaseException
*/
public function update(?array $set = null, $where = null, ?int $limit = null): bool
public function update($set = null, $where = null, ?int $limit = null): bool
{
if ($set !== null) {
$this->set($set);
Expand Down
28 changes: 27 additions & 1 deletion tests/system/Database/Builder/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function setUp(): void
$this->db = new MockConnection([]);
}

public function testSimpleInsert()
public function testInsertArray()
{
$builder = $this->db->table('jobs');

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

public function testInsertObject()
{
$builder = $this->db->table('jobs');

$insertData = (object) [
'id' => 1,
'name' => 'Grocery Sales',
];
$builder->testMode()->insert($insertData, true);

$expectedSQL = 'INSERT INTO "jobs" ("id", "name") VALUES (1, \'Grocery Sales\')';
$expectedBinds = [
'id' => [
1,
true,
],
'name' => [
'Grocery Sales',
true,
],
];

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
$this->assertSame($expectedBinds, $builder->getBinds());
}

public function testThrowsExceptionOnNoValuesSet()
{
$builder = $this->db->table('jobs');
Expand Down
28 changes: 26 additions & 2 deletions tests/system/Database/Builder/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,35 @@ protected function setUp(): void
$this->db = new MockConnection([]);
}

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

$builder->testMode()->where('id', 1)->update(['name' => 'Programmer'], null, null);
$data = ['name' => 'Programmer'];
$builder->testMode()->where('id', 1)->update($data, null, null);

$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
$expectedBinds = [
'id' => [
1,
true,
],
'name' => [
'Programmer',
true,
],
];

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledUpdate()));
$this->assertSame($expectedBinds, $builder->getBinds());
}

public function testUpdateObject()
{
$builder = new BaseBuilder('jobs', $this->db);

$data = (object) ['name' => 'Programmer'];
$builder->testMode()->where('id', 1)->update($data, null, null);

$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
$expectedBinds = [
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ BREAKING

- Now ``Services::request()`` returns ``IncomingRequest`` or ``CLIRequest``.
- 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.
- The method signature of ``BaseBuilder.php::insert()`` and ``BaseBuilder.php::update()`` have been changed. The ``?array`` typehint on the ``$set`` parameter was removed.

Enhancements
************
Expand Down