Skip to content

Commit

Permalink
Merge pull request #15 from nox7/1.4.1
Browse files Browse the repository at this point in the history
Do not update the PRIMARY KEY
  • Loading branch information
nox7 authored Aug 30, 2021
2 parents 496a4e7 + d20699a commit 8fcd4b0
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/ORM/Abyss.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public function buildSaveQuery(
): array {
$model = $classInstance->getModel();
$tableName = $model->getName();
$primaryKeyName = $this->getPrimaryKey($model);
$columnNameList = [];
$columnValues = [];
$rawQueryColumnValues = [];
Expand Down Expand Up @@ -326,7 +327,11 @@ public function buildSaveQuery(

foreach($columnNameList as $columnName){
$columnNameListAsMySQLSyntax .= sprintf("`%s`,", $columnName);
$columnUpdateMySQLSyntax .= sprintf("`%s` = VALUES(`%s`),", $columnName, $columnName);

// Do not update the primary key
if ($columnName !== $primaryKeyName) {
$columnUpdateMySQLSyntax .= sprintf("`%s` = VALUES(`%s`),", $columnName, $columnName);
}
}

// Remove the trailing commas
Expand Down Expand Up @@ -455,8 +460,8 @@ public function deleteRowByPrimaryKey(ModelInstance $classInstance): void{
}

/**
* Syncs all models to the database
*/
* Syncs all models to the database
*/
public function syncModels(): void{
$fileNames = array_diff(scandir(self::$modelsDirectory), ['.','..']);
foreach ($fileNames as $fileName){
Expand Down Expand Up @@ -565,8 +570,8 @@ protected function doesTableExist(string $tableName): bool{
*/
protected function isColumnAUniqueIndex(string $tableName, string $columnName): bool{
$result = $this->getConnection()->query(sprintf(
"SHOW INDEXES FROM `%s` WHERE `Column_name`='%s' AND Non_unique=0 AND Key_name != \"PRIMARY\"",
$tableName, $columnName
"SHOW INDEXES FROM `%s` WHERE `Column_name`='%s' AND Non_unique=0 AND Key_name != \"PRIMARY\"",
$tableName, $columnName
)
);
return $result->num_rows > 0;
Expand Down Expand Up @@ -753,8 +758,8 @@ protected function updateExistingTable(MySQLModelInterface $model): void{
}

/**
* Creates a table following a model
*/
* Creates a table following a model
*/
protected function createNewTable(MySQLModelInterface $model): void{
$connection = $this->getConnection();
$tableName = $model->getName();
Expand Down

0 comments on commit 8fcd4b0

Please sign in to comment.