更新字段.updateColumn

Testing Is Documentation

tests/Database/Update/UpdateColumnTest.php更新字段.updateColumn - 图1

Uses

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

updateColumn 基本用法

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

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "UPDATE `test_query` SET `test_query`.`name` = :pdonamedparameter_name WHERE `test_query`.`id` = :test_query_id",
  7. {
  8. "pdonamedparameter_name": [
  9. "小小小鸟,怎么也飞不高。"
  10. ],
  11. "test_query_id": [
  12. 503
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->sql()
  23. ->table('test_query')
  24. ->where('id', 503)
  25. ->updateColumn('name', '小小小鸟,怎么也飞不高。')
  26. )
  27. );
  28. }

updateColumn 支持表达式

  1. public function testExpression(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "UPDATE `test_query` SET `test_query`.`name` = concat(`test_query`.`value`,`test_query`.`name`) 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. ->updateColumn('name', Condition::raw('concat([value],[name])'))
  23. )
  24. );
  25. }