更新字段递减.updateDecrease

Testing Is Documentation

tests/Database/Update/UpdateDecreaseTest.php更新字段递减.updateDecrease - 图1

Uses

  1. <?php
  2. use Leevel\Database\Condition;
  3. use Tests\Database\DatabaseTestCase as TestCase;

updateDecrease 基本用法

更新成功后,返回影响行数,updateDecrease 实际上调用的是 updateColumn 方法。

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "UPDATE `test_query` SET `test_query`.`num` = `test_query`.`num`-3 WHERE `test_query`.`id` = :test_query_id",
  7. {
  8. "test_query_id": [
  9. 503
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->sql()
  20. ->table('test_query')
  21. ->where('id', 503)
  22. ->updateDecrease('num', 3)
  23. )
  24. );
  25. }

updateDecrease 支持参数绑定

  1. public function testBind(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "UPDATE `test_query` SET `test_query`.`num` = `test_query`.`num`-3 WHERE `test_query`.`id` = ?",
  7. [
  8. 503
  9. ],
  10. false
  11. ]
  12. eot;
  13. $this->assertSame(
  14. $sql,
  15. $this->varJson(
  16. $connect
  17. ->sql()
  18. ->table('test_query')
  19. ->where('id', Condition::raw('?'))
  20. ->updateDecrease('num', 3, [503])
  21. )
  22. );
  23. }