Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HUGE BUG: update() function updates all records if id is empty #4617

Closed
thewhistler1 opened this issue Apr 26, 2021 · 4 comments
Closed

HUGE BUG: update() function updates all records if id is empty #4617

thewhistler1 opened this issue Apr 26, 2021 · 4 comments

Comments

@thewhistler1
Copy link

Example code:

$params = array(
     'guid' => 12345
);
$employerModel->update($employer_id,$params);

The variable $employer_id was null for whatever reason and so the resulting query was this:

update employers set guid = '12345', updated_at = '2021-04-26 06:34:13';

This resulted in the function updating ALL records instead of no records.

CodeIgniter version: 4.1.1

Expected behavior -
I would expect the same behavior that results when you write the query manually:
update employers set guid = '12345', updated_at = '2021-04-26 06:34:13' WHERE employer_id = '';

The above query would not update any records unless you indeed had a employer with a null id. So I think the appropriate outcome would be to not update any records.

Context

  • OS: Ubuntu 20.04
  • Web server Apache/2.4.41 (Ubuntu)
  • PHP version PHP 7.4.3
@thewhistler1 thewhistler1 added the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 26, 2021
@musmanikram
Copy link
Contributor

musmanikram commented Apr 27, 2021

Indeed it's a HUGE bug and by mistake, but I assume it's intentional to update all records at once.
But on the other hand, if some passes a variable with a null value by mistake can mess up the whole table.

To make it defensive I think it should throw an exception. Will create a PR and let see what others think about this.

@iRedds
Copy link
Collaborator

iRedds commented Apr 28, 2021

This is not a bug. You want the framework to read your mind.

The update($id, $data) method of the model is equivalent to code

->whereIn('primary_key', [$id])->set($data)->update();

The code was made universal. Therefore, when $id = null, the condition is not added so that all records can be updated.

Your expected behavior will break working code for other developers.

You can rewrite the update method to achieve the behavior you want.
Or add conditions to the request like

->whereIn('employer_id', [$employer_id])->update(null, $params);

Or add a condition to the code

if ($employer_id !== null)
{
    ->update($employer_id, $params);
}

@paulbalandan paulbalandan removed the bug Verified issues on the current code behavior or pull requests that will fix them label May 3, 2021
@musmanikram
Copy link
Contributor

musmanikram commented May 3, 2021

@thewhistler1 I think @paulbalandan have a pretty much summarised why it's not a bug.
Please read this .

@paulbalandan Thanks again mate

@lonnieezell
Copy link
Member

I would also point out that is what regression tests are for - keeping us from shooting ourselves in the foot with dangerous code :)

This is not a code as pointed out, and is functioning as intended. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants