1

I dont recall exactly how, but I do remember being able to add one to an integer column in mysql with a query without knowing the value of that integer column...

something like

update table set column=column+1 where id=1

I am wondering, how can I do the above query with CakePHP 3

Jeffrey L. Roberts
  • 2,844
  • 5
  • 34
  • 69

1 Answers1

2

You can use \Cake\ORM\Table::updateAll() (or the underlying \Cake\Database\Query::update()), and use an expression to generate the arithmetic operation part, something like:

$additionExpression = $Table->query()->newExpr('column + 1');

$affectedRows = $Table->updateAll(
    ['column' => $additionExpression],
    ['id' => 1]
);

Or a little more complex in case you want to make use of automatic identifier quoting:

$additionExpression = $Table
    ->query()
    ->newExpr()
    ->add([
        new \Cake\Database\Expression\IdentifierExpression('column'),
        '1'
    ])
    ->setConjunction('+'); // tieWith() before CakePHP 3.4

See also

ndm
  • 59,784
  • 9
  • 71
  • 110