更新数据.update

Testing Is Documentation

tests/Database/Update/UpdateTest.php更新数据.update - 图1

Uses

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

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. ->update(['name' => '小猪'])
  26. )
  27. );
  28. }

update 更新指定条数

  1. public function testWithLimit(): 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 LIMIT 5",
  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. ->limit(5)
  26. ->update(['name' => '小猪'])
  27. )
  28. );
  29. }

update 更新排序

  1. public function testWithOrderBy(): 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 ORDER BY `test_query`.`id` DESC",
  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. ->orderBy('id desc')
  26. ->update(['name' => '小猪'])
  27. )
  28. );
  29. }

update 更新排序和指定条数

  1. public function testWithOrderAndLimit(): 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 ORDER BY `test_query`.`id` DESC LIMIT 2",
  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. ->orderBy('id desc')
  26. ->limit(2)
  27. ->update(['name' => '小猪'])
  28. )
  29. );
  30. }

update 连表更新

  1. public function testWithJoin(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "UPDATE `test_query` `t` INNER JOIN `test_query_subsql` `h` ON `t`.`id` = `h`.`value` SET `t`.`name` = :pdonamedparameter_name WHERE `t`.`id` = :t_id",
  7. {
  8. "pdonamedparameter_name": [
  9. "小猪"
  10. ],
  11. "t_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 as t')
  24. ->join('test_query_subsql as h', '', 't.id', '=', Condition::raw('[value]'))
  25. ->where('id', 503)
  26. ->update(['name' => '小猪'])
  27. )
  28. );
  29. }

update 更新参数绑定

  1. public function testBind(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "UPDATE `test_query` SET `test_query`.`name` = :hello,`test_query`.`value` = :pdopositional2namedparameter_0 WHERE `test_query`.`id` = :test_query_id",
  7. {
  8. "pdopositional2namedparameter_0": [
  9. "小牛逼"
  10. ],
  11. "test_query_id": [
  12. 503
  13. ],
  14. "hello": "hello world!"
  15. },
  16. false
  17. ]
  18. eot;
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->sql()
  24. ->table('test_query')
  25. ->where('id', 503)
  26. ->bind(['小牛逼'])
  27. ->update(
  28. [
  29. 'name' => Condition::raw(':hello'),
  30. 'value' => Condition::raw('?'),
  31. ],
  32. [
  33. 'hello' => 'hello world!',
  34. ]
  35. )
  36. )
  37. );
  38. }

update 更新支持表达式

  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. ->update([
  23. 'name' => Condition::raw('concat([value],[name])'),
  24. ])
  25. )
  26. );
  27. }