Skip to content

Possible bug: Model functions (doDelete, doFirst, etc..) ignores zero as id #9577

Open
@ECode16

Description

@ECode16

PHP Version

8.3

CodeIgniter4 Version

4.1.6

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

No response

What happened?

doDelete from Model.php assumes there already is where query when $id is falsy value. I get why null and empty arrays are ignored but why zeros as well? (empty string as primary key is some black magic fuckery from my nightmares thought)

if (! in_array($id, [null, '', 0, '0', []], true)) {
    $builder = $builder->whereIn($this->primaryKey, $id);
}

Steps to Reproduce

in model
$this->delete(0);

Expected Output

deletion of row with primary key 0

Anything else?

The only related thing I found in user guide is validation rule in Using CodeIgniter’s Model
'id' => 'max_length[19]|is_natural_no_zero'.
Tried some googling if there's reason for zero to be invalid ID but only found some question about why auto-increments start at 1.

There was an attempt to modify this behaviour back in 2021 but it went stale and then closed.

It was introduced in 4.0.5 as implicit conversion of $id to bool and before the check was:

if (! empty($id) && (is_numeric($id) || is_string($id)))
{
    $id = [$id];
}

Sorry if I missed something already written about this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions