Skip to content

Commit b3f1598

Browse files
authored
Merge pull request #9 from NamelessCoder/feature/update-mode
[FEATURE] Add "update" mode
2 parents 4047b88 + fbb08f2 commit b3f1598

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ In this case, create a file `conf/site_dev.yaml` for the local setup. It looks l
6363
Other files for staging, production etc. can be created accordingly.
6464

6565
The option `mode` describes whether to truncate the database table before the entries are added.
66-
but could also be set to `append`.
66+
but could also be set to `append` or `update`. If set to `append` the entries will be written as
67+
pure insert without regard whether the record already exists.
68+
69+
If `mode` is set to `replace` the table is first truncated, then entries are inserted.
70+
71+
If `mode` is set to `update` the following happens:
72+
73+
* A check is made if the entry contains a `uid` property. If it does not, the entry is inserted as
74+
a new record (like it would happen if `mode` was set to `append`).
75+
* If entry does contain a `uid` property, the script checks if the record exist in the table and
76+
if it does, an SQL update is done to update the record. If it does not exist, it is inserted.
6777

6878
The file accepts more than one table, thus, all language records could be added as well, however
6979
this could be done in a generic `sites.yaml` file which works for all environments.

src/Command/SiteImportCommandController.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@ public function fromFileCommand($file)
4242
$this->outputLine('Emptied database table "' . $config['table'] . '"');
4343
}
4444
foreach ($config['entries'] as $entry) {
45-
$conn->insert($config['table'], $entry);
46-
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
45+
if ($mode === 'update' && isset($entry['uid'])) {
46+
$identifiers = ['uid' => $entry['uid']];
47+
if ($conn->count('uid', $config['table'], $identifiers)) {
48+
$conn->update($config['table'], $entry, $identifiers);
49+
$this->outputLine('Updated (mode=update, entry has uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
50+
} else {
51+
$conn->insert($config['table'], $entry);
52+
$this->outputLine('Added (mode=update, entry does not have uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
53+
}
54+
} else {
55+
$conn->insert($config['table'], $entry);
56+
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
57+
}
4758
}
4859
} else {
4960
if (!($GLOBALS['TYPO3_DB'] instanceof DatabaseConnection)) {
@@ -57,8 +68,19 @@ public function fromFileCommand($file)
5768
$this->outputLine('Emptied database table "' . $config['table'] . '"');
5869
}
5970
foreach ($config['entries'] as $entry) {
60-
$conn->exec_INSERTquery($config['table'], $entry);
61-
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
71+
if ($mode === 'update' && isset($entry['uid'])) {
72+
$condition = sprintf('uid = %d', $entry['uid']);
73+
if ($conn->exec_SELECTcountRows('uid', $config['table'], $condition)) {
74+
$conn->exec_UPDATEquery($config['table'], $condition, $entry);
75+
$this->outputLine('Updated (mode=update, entry has uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
76+
} else {
77+
$conn->exec_INSERTquery($config['table'], $entry);
78+
$this->outputLine('Added (mode=update, entry does not have uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
79+
}
80+
} else {
81+
$conn->exec_INSERTquery($config['table'], $entry);
82+
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
83+
}
6284
}
6385
}
6486
}

0 commit comments

Comments
 (0)