Skip to content

Commit 90623f8

Browse files
committed
upsert updateFields
1 parent 8a2bd0a commit 90623f8

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

packages/database/src/Manager/WriterManager.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public function upsert(
220220
string $table,
221221
array|object $data,
222222
array|string $keys,
223+
array|null $updateFields = null,
223224
array $options = [],
224225
): StatementInterface {
225226
$keys = (array) $keys;
@@ -236,10 +237,13 @@ public function upsert(
236237
$table = (string) $query->quoteName($table);
237238
$data = TypeCast::toArray($data);
238239
$allFields = array_keys($data);
239-
$valueFields = array_filter($allFields, fn($field) => !in_array($field, $keys, true));
240+
// $valueFields = array_filter($allFields, fn($field) => !in_array($field, $keys, true));
240241

242+
$updateFields ??= array_filter($allFields, fn($field) => !in_array($field, $keys, true));
243+
244+
$quotedUpdateFields = $query->qnMultiple($updateFields);
241245
$quotedFields = $query->qnMultiple($allFields);
242-
$quotedValueFields = $query->qnMultiple($valueFields);
246+
// $quotedValueFields = $query->qnMultiple($valueFields);
243247
$quotedKeys = $query->qnMultiple($keys);
244248

245249
$columns = $query->clause('()', $quotedFields, ',');
@@ -256,39 +260,39 @@ public function upsert(
256260

257261
switch ($platformName) {
258262
case AbstractPlatform::MYSQL:
259-
$dupKeys = $query->clause('', [], ',');
263+
$updateActions = $query->clause('', [], ',');
260264

261-
foreach ($quotedValueFields as $key) {
262-
$dupKeys->append("$key = VALUES($key)");
265+
foreach ($quotedUpdateFields as $key) {
266+
$updateActions->append("$key = VALUES($key)");
263267
}
264268

265269
$query->sql(
266270
<<<SQL
267271
INSERT INTO $table $columns
268272
VALUES $values
269273
ON DUPLICATE KEY UPDATE
270-
$dupKeys
274+
$updateActions
271275
SQL
272276
);
273277
break;
274278

275279
case AbstractPlatform::POSTGRESQL:
276-
$dupKeys = $query->clause('()', [], ',');
280+
$updateActions = $query->clause('()', [], ',');
277281

278282
foreach ($quotedKeys as $key) {
279-
$dupKeys->append($key);
283+
$updateActions->append($key);
280284
}
281285

282286
$excludeKeys = $query->clause('', [], ',');
283287

284-
foreach ($quotedValueFields as $key) {
288+
foreach ($quotedUpdateFields as $key) {
285289
$excludeKeys->append("$key = EXCLUDED.$key");
286290
}
287291

288292
$query->sql(
289293
<<<SQL
290294
INSERT INTO $table $columns
291-
VALUES $values ON CONFLICT $dupKeys DO UPDATE SET
295+
VALUES $values ON CONFLICT $updateActions DO UPDATE SET
292296
$excludeKeys
293297
SQL
294298
);
@@ -318,7 +322,7 @@ public function upsert(
318322

319323
$updateSet = $query->clause('', [], ',');
320324

321-
foreach ($valueFields as $k) {
325+
foreach ($updateFields as $k) {
322326
$v = $data[$k];
323327

324328
if ($v instanceof RawWrapper) {

packages/orm/src/EntityMapper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ function () use ($initData, $data, $item, $options, $condFields, $conditions) {
997997
public function upsert(
998998
array|object $item,
999999
array|string|null $condFields = null,
1000+
array|null $updateFields = null,
10001001
ORMOptions $options = new ORMOptions()
10011002
): ?StatementInterface {
10021003
if ($item === []) {
@@ -1043,6 +1044,7 @@ public function upsert(
10431044
$metadata->getTableName(),
10441045
$data,
10451046
$condFields,
1047+
$updateFields,
10461048
[
10471049
'updateNulls' => $updateNulls,
10481050
]

packages/orm/src/ORMProxyTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,10 @@ public function upsert(
385385
string $entityClass,
386386
array|object $item,
387387
array|string|null $condFields = null,
388+
array|null $updateFields = null,
388389
ORMOptions $options = new ORMOptions()
389390
): ?StatementInterface {
390-
return $this->mapper($entityClass)->upsert($item, $condFields, $options);
391+
return $this->mapper($entityClass)->upsert($item, $condFields, $updateFields, $options);
391392
}
392393

393394
/**

0 commit comments

Comments
 (0)