动态查询.find.findStart.findBy.findAllBy

Testing Is Documentation

tests/Database/Read/FindDynamicsTest.php动态查询.find.findStart.findBy.findAllBy - 图1

Uses

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

find[0-9] 查询指定条数数据

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` LIMIT 0,10",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->sql()
  16. ->table('test_query')
  17. ->find10()
  18. )
  19. );
  20. }

find[0-9]start[0-9] 查询指定开始位置指定条数数据

  1. public function testFindStart(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` LIMIT 3,10",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->sql()
  16. ->table('test_query')
  17. ->find10start3(),
  18. 1
  19. )
  20. );
  21. }

findBy 字段条件查询单条数据

方法遵循驼峰法,相应的字段为下划线。

  1. public function testFindByField(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`user_name` = :test_query_user_name LIMIT 1",
  7. {
  8. "test_query_user_name": [
  9. "1111"
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->sql()
  20. ->table('test_query')
  21. ->findByUserName('1111'),
  22. 2
  23. )
  24. );
  25. }

findBy 字段条件查询单条数据,字段保持原样

方法遵循驼峰法,尾巴加一个下划线 _,相应的字段保持原样。

  1. public function testFindByFieldWithoutCamelize(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`UserName` = :test_query_UserName LIMIT 1",
  7. {
  8. "test_query_UserName": [
  9. "1111"
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->sql()
  20. ->table('test_query')
  21. ->findByUserName_('1111'),
  22. 3
  23. )
  24. );
  25. }

findAllBy 字段条件查询多条数据,字段保持原样

方法遵循驼峰法,相应的字段为下划线。

  1. public function testTestfindAllByField(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`user_name` = :test_query_user_name AND `test_query`.`sex` = :test_query_sex",
  7. {
  8. "test_query_user_name": [
  9. "1111"
  10. ],
  11. "test_query_sex": [
  12. "222"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->sql()
  23. ->table('test_query')
  24. ->findAllByUserNameAndSex('1111', '222'),
  25. 4
  26. )
  27. );
  28. }

findAllBy 字段条件查询多条数据,字段保持原样

方法遵循驼峰法,尾巴加一个下划线 _,相应的字段保持原样。

  1. public function testTestfindAllByFieldWithoutCamelize(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`UserName` = :test_query_UserName AND `test_query`.`Sex` = :test_query_Sex",
  7. {
  8. "test_query_UserName": [
  9. "1111"
  10. ],
  11. "test_query_Sex": [
  12. "222"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->sql()
  23. ->table('test_query')
  24. ->findAllByUserNameAndSex_('1111', '222'),
  25. 5
  26. )
  27. );
  28. }