Skip to content

feat: Add incrementAll() and decrementAll() methods to BaseBuilder and other driver specific builders#10140

Open
patel-vansh wants to merge 7 commits intocodeigniter4:4.8from
patel-vansh:feat/increment-multiple
Open

feat: Add incrementAll() and decrementAll() methods to BaseBuilder and other driver specific builders#10140
patel-vansh wants to merge 7 commits intocodeigniter4:4.8from
patel-vansh:feat/increment-multiple

Conversation

@patel-vansh
Copy link
Copy Markdown
Contributor

@patel-vansh patel-vansh commented Apr 25, 2026

Description
This PR adds two new functions in BaseBuilder.phpincrementAll() and decrementAll(). These two functions enable devs to increment/decrement multiple rows at once atomically.

Also, the increment() and decrement() methods now internally call incrementAll() and decrementAll() respectively to prevent duplicating the logic.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Apr 25, 2026
Copy link
Copy Markdown
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered adding a separate incrementAll (or other wording) that will take care of the array, so to minimize the BC break?

PS. The random test failure seems legitimate. I'll deal with that later.

@michalsn
Copy link
Copy Markdown
Member

I also think that incrementMany() or incrementAll() would probably work better.

@paulbalandan paulbalandan added the breaking change Pull requests that may break existing functionalities label Apr 25, 2026
@patel-vansh
Copy link
Copy Markdown
Contributor Author

patel-vansh commented Apr 25, 2026

Yeah, that would avoid BC. I guess incrementAll would be the new method.

Maybe the current increment method calls incrementAll internally? So, there's no duplication of any logic.
@paulbalandan and @michalsn

@michalsn
Copy link
Copy Markdown
Member

Maybe the current increment method calls incrementAll internally? So, there's no duplication of any logic.

I see why not.

@patel-vansh patel-vansh changed the title feat: Add support to increment/decrement multiple columns at once feat: Add incrementAll() and decrementAll() methods to BaseBuilder and other driver specific builders Apr 25, 2026
@paulbalandan paulbalandan removed the breaking change Pull requests that may break existing functionalities label Apr 25, 2026
@patel-vansh
Copy link
Copy Markdown
Contributor Author

Not sure why PHPStan analysis have failed.

@paulbalandan
Copy link
Copy Markdown
Member

You apparently fixed (or renamed) the affected code. Just run composer phpstan:baseline to update the baseline.

Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supporting a more convenient API would be nice:

incrementAll(['one', 'two'], 2)
incrementAll(['one' => 2, 'two' => 3])

What do you think?

Something like:

public function incrementAll(array $columns, int $value = 1): bool
{
    $fields = [];

    if (array_is_list($columns)) {
        foreach ($columns as $column) {
            $column          = $this->db->protectIdentifiers($column);
            $fields[$column] = "{$column} + {$value}";
        }
    } else {
        foreach ($columns as $column => $value) {
            if (! is_int($value)) {
                throw new TypeError(...);
            }

            $column          = $this->db->protectIdentifiers($column);
            $fields[$column] = "{$column} + {$value}";
        }
    }
    
    // ...
}

Comment on lines +2944 to +2947
foreach ($columns as $col => $val) {
$col = $this->db->protectIdentifiers($col);
$fields[$col] = "{$col} + {$val}";
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we no longer have runtime type enforcement for each value, we should add a check manually. Something like:

if (! is_int($val)) {
    throw new TypeError(sprintf(
        'Argument #1 ($columns) must contain only int values, %s given for "%s".',
        get_debug_type($val),
        (string) $col,
    ));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants