Skip to content
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
4 changes: 2 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1673,12 +1673,12 @@ protected function _limit(string $sql, bool $offsetIgnore = false): string
* Allows key/value pairs to be set for insert(), update() or replace().
*
* @param string|array|object $key Field name, or an array of field/value pairs
* @param string $value Field value, if $key is a single field
* @param mixed $value Field value, if $key is a single field
* @param bool $escape Whether to escape values and identifiers
*
* @return $this
*/
public function set($key, ?string $value = '', bool $escape = null)
public function set($key, $value = '', bool $escape = null)
{
$key = $this->objectToArray($key);

Expand Down
4 changes: 4 additions & 0 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ protected function _attributeType(array &$attributes)
$attributes['TYPE'] = 'DATETIME';
break;

case 'BOOLEAN':
$attributes['TYPE'] = 'BIT';
break;

default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ protected function _attributeType(array &$attributes)
$attributes['TYPE'] = 'TEXT';
break;

case 'BOOLEAN':
$attributes['TYPE'] = 'INT';
break;

default:
break;
}
Expand Down
8 changes: 4 additions & 4 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,13 @@ public function builder(?string $table = null)
* data here. This allows it to be used with any of the other
* builder methods and still get validated data, like replace.
*
* @param mixed $key Field name, or an array of field/value pairs
* @param string|null $value Field value, if $key is a single field
* @param bool|null $escape Whether to escape values and identifiers
* @param mixed $key Field name, or an array of field/value pairs
* @param mixed $value Field value, if $key is a single field
* @param bool|null $escape Whether to escape values and identifiers
*
* @return $this
*/
public function set($key, ?string $value = '', ?bool $escape = null)
public function set($key, $value = '', ?bool $escape = null)
{
$data = is_array($key) ? $key : [$key => $value];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function up()
'type_double' => ['type' => 'DOUBLE', 'null' => true],
'type_decimal' => ['type' => 'DECIMAL', 'constraint' => '18,4', 'null' => true],
'type_blob' => ['type' => 'BLOB', 'null' => true],
'type_boolean' => ['type' => 'BOOLEAN', 'null' => true],
];

if ($this->db->DBDriver === 'Postgre') {
Expand Down
4 changes: 3 additions & 1 deletion tests/_support/Database/Seeds/CITestSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function run()
'type_datetime' => '2020-06-18T05:12:24.000+02:00',
'type_timestamp' => '2019-07-18T21:53:21.000+02:00',
'type_bigint' => 2342342,
'type_boolean' => 1,
],
],
];
Expand All @@ -110,7 +111,8 @@ public function run()
}

if ($this->db->DBDriver === 'Postgre') {
$data['type_test'][0]['type_time'] = '15:22:00';
$data['type_test'][0]['type_time'] = '15:22:00';
$data['type_test'][0]['type_boolean'] = true;
unset(
$data['type_test'][0]['type_enum'],
$data['type_test'][0]['type_set'],
Expand Down
74 changes: 74 additions & 0 deletions tests/system/Database/Builder/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,80 @@ public function testUpdateWithSet()
$this->assertSame($expectedBinds, $builder->getBinds());
}

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

$builder->testMode()->set('age', 22)->where('id', 1)->update(null, null, null);

$expectedSQL = 'UPDATE "jobs" SET "age" = 22 WHERE "id" = 1';
$expectedBinds = [
'age' => [
22,
true,
],
'id' => [
1,
true,
],
];

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

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

$builder->testMode()->set('manager', true)->where('id', 1)->update(null, null, null);

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

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

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

$builder->testMode()->set(['name' => 'Programmer', 'age' => 22, 'manager' => true])->where('id', 1)->update(null, null, null);

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

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

public function testUpdateThrowsExceptionWithNoData()
{
$builder = new BaseBuilder('jobs', $this->db);
Expand Down
19 changes: 19 additions & 0 deletions tests/system/Database/Live/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,23 @@ public function testSetWithoutEscape()
'description' => 'Developer',
]);
}

public function testSetWithBoolean()
{
$this->db->table('type_test')
->set('type_boolean', false)
->update();

$this->seeInDatabase('type_test', [
'type_boolean' => false,
]);

$this->db->table('type_test')
->set('type_boolean', true)
->update();

$this->seeInDatabase('type_test', [
'type_boolean' => true,
]);
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See all the changes.
.. toctree::
:titlesonly:

v4.2.0
v4.1.4
v4.1.3
v4.1.2
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/database/query_builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ Class Reference
.. php:method:: set($key[, $value = ''[, $escape = null]])

:param mixed $key: Field name, or an array of field/value pairs
:param string $value: Field value, if $key is a single field
:param mixed $value: Field value, if $key is a single field
:param bool $escape: Whether to escape values and identifiers
:returns: ``BaseBuilder`` instance (method chaining)
:rtype: ``BaseBuilder``
Expand Down
12 changes: 12 additions & 0 deletions user_guide_src/source/installation/upgrade_420.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#############################
Comment thread
paulbalandan marked this conversation as resolved.
Upgrading from 4.1.3 to 4.2.0
#############################

**Changes for set() method in BaseBuilder and Model class**

The casting for the ``$value`` parameter has been removed to fix a bug where passing parameters as array and string
to the ``set()`` method were handled differently. If you extended the ``BaseBuilder`` class or ``Model`` class yourself
and modified the ``set()`` method, then you need to change its definition from
``public function set($key, ?string $value = '', ?bool $escape = null)`` to
``public function set($key, $value = '', ?bool $escape = null)``.