Skip to content

Commit

Permalink
Update BaseModel.php
Browse files Browse the repository at this point in the history
Improve shouldUpdate method (correct)

Enhance the shouldUpdate method to ensure it doesn't incorrectly trigger an update operation when a user provides a manually generated ID that does not exist in the system.
  • Loading branch information
ibnulhusainan authored Dec 24, 2023
1 parent fe0fa71 commit 7c6a63f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\Pager;
use CodeIgniter\Validation\ValidationInterface;
use CodeIgniter\Validation\StrictRules\Rules;
use Config\Services;
use InvalidArgumentException;
use ReflectionClass;
Expand Down Expand Up @@ -725,15 +726,15 @@ public function save($row): bool
* It checks if the ID is not empty and does not already exist in the table.
* If this method returns false, an insert operation will be executed.
*
* @param array|object $data Data
* @param array|object $row Data
* @phpstan-param row_array|object $row
* @return bool
*/
protected function shouldUpdate($data): bool
protected function shouldUpdate($row): bool
{
$shouldUpdate = false;

$id = $this->getIdValue($data);
$id = $this->getIdValue($row);
if (!empty($id)) {
$ci_rules = new Rules;
if (!$ci_rules->is_unique($id, "`{$this->table}`.`{$this->primaryKey}`", [])) {

Check failure on line 740 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Access to an undefined property CodeIgniter\BaseModel::$primaryKey.

Check failure on line 740 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Access to an undefined property CodeIgniter\BaseModel::$table.
Expand Down

0 comments on commit 7c6a63f

Please sign in to comment.