Skip to content

Commit 65fce48

Browse files
committed
Add logic to updateFields() to add additional fields for update
This allows you to add a field to the SET part of the query without defining all the other fields. The default is to update all the fields except the constraint fields. This adds a field in addition to these.
1 parent 6a3d490 commit 65fce48

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

system/Database/BaseBuilder.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class BaseBuilder
159159
* and is reset by resetWrite()
160160
*
161161
* @phpstan-var array{
162+
* updateFieldsAdditional?: array,
162163
* updateFields?: array,
163164
* constraints?: array,
164165
* fromQuery?: string,
@@ -1863,15 +1864,15 @@ public function setAlias(string $alias): BaseBuilder
18631864
}
18641865

18651866
/**
1866-
* Sets update fields for updateBatch
1867+
* Sets update fields for upsert, update
18671868
*
18681869
* @param string|string[] $set
1869-
* @param bool $addToDefault future use
1870+
* @param bool $addToDefault adds update fields to the default ones
18701871
* @param array|null $ignore ignores items in set
18711872
*
18721873
* @return $this
18731874
*/
1874-
protected function updateFields($set, bool $addToDefault = false, ?array $ignore = null)
1875+
public function updateFields($set, bool $addToDefault = false, ?array $ignore = null)
18751876
{
18761877
if (! empty($set)) {
18771878
if (! is_array($set)) {
@@ -1888,13 +1889,20 @@ protected function updateFields($set, bool $addToDefault = false, ?array $ignore
18881889
}
18891890

18901891
if ($ignore === null || ! in_array($key, $ignore, true)) {
1891-
$this->QBOptions['updateFields'][$this->db->protectIdentifiers($key)] = $value;
1892+
if ($addToDefault) {
1893+
$this->QBOptions['updateFieldsAdditional'][$this->db->protectIdentifiers($key)] = $value;
1894+
} else {
1895+
$this->QBOptions['updateFields'][$this->db->protectIdentifiers($key)] = $value;
1896+
}
18921897
}
18931898
}
1894-
}
18951899

1896-
return $this;
1897-
}
1900+
if ($addToDefault === false && isset($this->QBOptions['updateFieldsAdditional'], $this->QBOptions['updateFields'])) {
1901+
$this->QBOptions['updateFields'] = array_merge($this->QBOptions['updateFields'], $this->QBOptions['updateFieldsAdditional']);
1902+
1903+
unset($this->QBOptions['updateFieldsAdditional']);
1904+
}
1905+
}
18981906

18991907
/**
19001908
* Sets constraints for batch upsert, update

0 commit comments

Comments
 (0)