dongqiao1888 2018-01-17 20:56
浏览 10
已采纳

使用CakePHP 3中的查询将一个添加到整数列

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

  • 写回答

1条回答 默认 最新

  • dongshi6710 2018-01-17 23:12
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度