Skip to content

Commit

Permalink
Update 'updated_at' when enabled in replace()
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jun 8, 2021
1 parent 34d74c7 commit 19e92c1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
4 changes: 4 additions & 0 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,10 @@ public function replace(array $data = null, bool $returnSQL = false)
return false;
}

if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, (array) $data)) {
$data[$this->updatedField] = $this->setDate();
}

return $this->doReplace($data, $returnSQL);
}

Expand Down
32 changes: 19 additions & 13 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,16 @@ public function decrement(string $column, int $value = 1)
//--------------------------------------------------------------------

/**
* Replace
*
* Compiles an replace into string and runs the query.
* Compiles a replace into string and runs the query.
* Because PostgreSQL doesn't support the replace into command,
* we simply do a DELETE and an INSERT on the first key/value
* combo, assuming that it's either the primary key or a unique key.
*
* @param array $set An associative array of insert values
*
* @return mixed
* @throws DatabaseException
* @internal param true $bool returns the generated SQL, false executes the query.
* @return mixed
*
* @throws DatabaseException
*/
public function replace(array $set = null)
{
Expand All @@ -160,24 +158,32 @@ public function replace(array $set = null)
if (CI_DEBUG) {
throw new DatabaseException('You must use the "set" method to update an entry.');
}
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd

return false; // @codeCoverageIgnore
}

$table = $this->QBFrom[0];
$set = $this->binds;

array_walk($set, static function (array &$item) {
$item = $item[0];
});

$key = array_key_first($set);
$value = $set[$key];

$builder = $this->db->table($table);
$exists = $builder->where("$key = $value", null, false)->get()->getFirstRow();
$exists = $builder->where($key, $value, true)->get()->getFirstRow();

if (empty($exists)) {
if (empty($exists) && $this->testMode) {
$result = $this->getCompiledInsert();
} elseif (empty($exists)) {
$result = $builder->insert($set);
} elseif ($this->testMode) {
$result = $this->where($key, $value, true)->getCompiledUpdate();
} else {
array_pop($set);
$result = $builder->update($set, "$key = $value");
array_shift($set);
$result = $builder->where($key, $value, true)->update($set);
}

unset($builder);
Expand Down
4 changes: 2 additions & 2 deletions system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ protected function _replace(string $table, array $keys, array $values): string
$bingo = [];

foreach ($common as $v) {
$k = array_search($v, $escKeyFields, true);
$k = array_search($v, $keys, true);

$bingo[$keyFields[$k]] = $binds[trim($values[$k], ':')];
$bingo[$keys[$k]] = $binds[trim($values[$k], ':')];
}

// Querying existing data
Expand Down
32 changes: 32 additions & 0 deletions tests/system/Models/ReplaceModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace CodeIgniter\Models;

use Tests\Support\Models\UserModel;

final class ReplaceModelTest extends LiveModelTestCase
{
public function testReplaceRespectsUseTimestamps(): void
{
$this->createModel(UserModel::class);

$data = [
'name' => 'Amanda Holmes',
'email' => 'amanda@holmes.com',
'country' => 'US',
];

$id = $this->model->insert($data);

$data['id'] = $id;
$data['country'] = 'UK';

$sql = $this->model->replace($data, true);
$this->assertStringNotContainsString('updated_at', $sql);

$this->model = $this->createModel(UserModel::class);
$this->setPrivateProperty($this->model, 'useTimestamps', true);
$sql = $this->model->replace($data, true);
$this->assertStringContainsString('updated_at', $sql);
}
}

0 comments on commit 19e92c1

Please sign in to comment.