Skip to content
Merged
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
33 changes: 22 additions & 11 deletions src/dba/AbstractModelFactory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@ public function save(AbstractModel $model): ?AbstractModel {

$query = "INSERT INTO " . $this->getMappedModelTable();
$vals = array_values($dict);

$keys = self::getMappedModelKeys($model);

if($vals[0] === -1 || $vals[0] === null){
array_splice($vals, 0, 1);
array_splice($keys, 0, 1);
}

$query .= " (" . implode(",", $keys) . ") ";
$placeHolder = " (" . implode(",", array_fill(0, count($keys), "?")) . ")";

Expand All @@ -144,16 +150,21 @@ public function save(AbstractModel $model): ?AbstractModel {
$stmt = $dbh->prepare($query);
$stmt->execute($vals);

$id = intval($dbh->lastInsertId());
if ($id != 0) {
$model->setId($id);
return $model;
}
else if ($model->getId() != 0) {
return $model;
if ($model->getId() === null || $model->getId() === -1) {
$id = intval($dbh->lastInsertId());
if ($id != 0) {
$model->setId($id);
return $model;
}
else if ($model->getId() != 0) {
return $model;
}
else {
return null;
}
}
else {
return null;
return $model;
}
}

Expand Down Expand Up @@ -361,6 +372,8 @@ public function massSave(array $models): bool|PDOStatement {
$keys = self::getMappedModelKeys($models[0]);
$query = "INSERT INTO " . $this->getMappedModelTable();

array_splice($keys, 0, 1);

$query .= " (" . implode(",", $keys) . ") ";
$placeHolder = " (" . implode(",", array_fill(0, count($keys), "?")) . ")";

Expand All @@ -371,10 +384,8 @@ public function massSave(array $models): bool|PDOStatement {
if ($x < sizeof($models) - 1) {
$query .= ", ";
}
if ($models[$x]->getId() === 0) {
$models[$x]->setId(null);
}
$dict = $models[$x]->getKeyValueDict();
array_splice($dict, 0, 1);
foreach ($dict as $val) {
$vals[] = $val;
}
Expand Down