Skip to content

Commit 4e6cd2b

Browse files
committed
fixed mapping of dict keys from mapped columns, including suggestions from copilot
1 parent f42acd3 commit 4e6cd2b

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

src/dba/AbstractModelFactory.class.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,19 @@ public function update(AbstractModel $model): PDOStatement {
228228

229229
$query = "UPDATE " . $this->getMappedModelTable() . " SET ";
230230

231+
$values = array_values($dict);
231232
$keys = self::getMappedModelKeys($model);
232-
$values = array();
233233

234234
for ($i = 0; $i < count($keys); $i++) {
235235
if ($i != count($keys) - 1) {
236-
$query = $query . $keys[$i] . "=?,";
237-
$values[] = $dict[$keys[$i]];
236+
$query .= $keys[$i] . "=?, ";
238237
}
239238
else {
240-
$query = $query . $keys[$i] . "=?";
241-
$values[] = $dict[$keys[$i]];
239+
$query .= $keys[$i] . "=?";
242240
}
243241
}
244242

245-
$query = $query . " WHERE " . $model->getPrimaryKey() . "=?";
243+
$query .= " WHERE " . $model->getPrimaryKey() . "=?";
246244
$values[] = $model->getPrimaryKeyValue();
247245

248246
$stmt = $this->getDB()->prepare($query);

src/dba/PaginationFilter.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function getQueryString(AbstractModelFactory $factory, bool $includeTable = fals
3636
$table = $factory->getMappedModelTable() . ".";
3737
}
3838

39-
$parts = array_map(fn($filter) => $filter->getQueryString(), $this->filters);
39+
$parts = array_map(fn($filter) => $filter->getQueryString($factory, true), $this->filters);
4040
//ex. SELECT hashTypeId, description, isSalted, isSlowHash FROM HashType
4141
// where (HashType.isSalted < 1) OR (HashType.isSalted = 1 and HashType.hashTypeId < 12600)
4242
// ORDER BY HashType.isSalted DESC, HashType.hashTypeId DESC LIMIT 25;
@@ -60,4 +60,4 @@ function getHasValue(): bool {
6060
}
6161
return true;
6262
}
63-
}
63+
}

src/dba/UpdateSet.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function getQuery(AbstractModelFactory $factory, bool $includeTable = false): st
1616
if ($includeTable) {
1717
$table = $factory->getMappedModelTable() . ".";
1818
}
19+
1920
return $table . AbstractModelFactory::getMappedModelKey($factory->getNullObject(), $this->key) . "=?";
2021
}
2122

src/dba/models/AbstractModelFactory.template.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class __MODEL_NAME__Factory extends AbstractModelFactory {
3535
* @param array $dict
3636
* @return __MODEL_NAME__
3737
*/
38-
function createObjectFromDict($pk, $dict): __MODEL_NAME__ {
38+
function createObjectFromDict($pk, $dict): __MODEL_NAME__ {__MODEL_MAPPING_DICT__
3939
return new __MODEL_NAME__(__MODEL__DICT2__);
4040
}
4141

src/dba/models/HealthCheckAgentFactory.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function getNullObject(): HealthCheckAgent {
3636
* @return HealthCheckAgent
3737
*/
3838
function createObjectFromDict($pk, $dict): HealthCheckAgent {
39+
$dict['end'] = $dict['htp_end'];
3940
return new HealthCheckAgent($dict['healthCheckAgentId'], $dict['healthCheckId'], $dict['agentId'], $dict['status'], $dict['cracked'], $dict['numGpus'], $dict['start'], $dict['end'], $dict['errors']);
4041
}
4142

src/dba/models/generator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,9 @@ function getTypingType($str, $nullable = false): string {
596596
$class = file_get_contents(dirname(__FILE__) . "/AbstractModelFactory.template.txt");
597597
$class = str_replace("__MODEL_NAME__", $NAME, $class);
598598
$class = str_replace("__MODEL_DBA_MAPPING__", (array_key_exists("dba_mapping", $MODEL_CONF) ? ($MODEL_CONF['dba_mapping'] ? 'True' : 'False') : 'False'), $class);
599-
$dict = array();
600-
$dict2 = array();
599+
$dict = [];
600+
$dict2 = [];
601+
$mapping = [];
601602
foreach ($COLUMNS as $COLUMN) {
602603
$col = $COLUMN['name'];
603604
if (sizeof($dict) == 0) {
@@ -607,10 +608,19 @@ function getTypingType($str, $nullable = false): string {
607608
else {
608609
$dict[] = "null";
609610
$dict2[] = "\$dict['$col']";
611+
if (array_key_exists("dba_mapping", $COLUMN) && $COLUMN['dba_mapping']) {
612+
$mapping[] = "\$dict['$col'] = \$dict['htp_$col'];";
613+
}
610614
}
611615
}
612616
$class = str_replace("__MODEL_DICT__", implode(", ", $dict), $class);
613617
$class = str_replace("__MODEL__DICT2__", implode(", ", $dict2), $class);
618+
if (count($mapping) > 0) {
619+
$class = str_replace("__MODEL_MAPPING_DICT__", "\n " . implode("\n ", $mapping), $class);
620+
}
621+
else {
622+
$class = str_replace("__MODEL_MAPPING_DICT__", "", $class);
623+
}
614624

615625
file_put_contents(dirname(__FILE__) . "/" . $NAME . "Factory.class.php", $class);
616626
}

0 commit comments

Comments
 (0)