数据库配置

我们可以在 option/database.php 文件中定义数据库连接。

引入相关类

  • use PDO;
  • use Tests\Database\DatabaseTestCase as TestCase;

    基本配置

数据库配置基本定义功能展示。

数据库配置

  1. protected function createDatabaseManager(): Manager
  2. {
  3. $container = new Container();
  4. $manager = new Manager($container);
  5. $this->assertInstanceof(IContainer::class, $manager->container());
  6. $this->assertInstanceof(Container::class, $manager->container());
  7. $option = new Option([
  8. 'database' => [
  9. 'default' => 'mysql',
  10. 'connect' => [
  11. 'mysql' => [
  12. 'driver' => 'mysql',
  13. 'host' => $GLOBALS['LEEVEL_ENV']['DATABASE']['MYSQL']['HOST'],
  14. 'port' => $GLOBALS['LEEVEL_ENV']['DATABASE']['MYSQL']['PORT'],
  15. 'name' => $GLOBALS['LEEVEL_ENV']['DATABASE']['MYSQL']['NAME'],
  16. 'user' => $GLOBALS['LEEVEL_ENV']['DATABASE']['MYSQL']['USER'],
  17. 'password' => $GLOBALS['LEEVEL_ENV']['DATABASE']['MYSQL']['PASSWORD'],
  18. 'charset' => 'utf8',
  19. 'options' => [
  20. PDO::ATTR_PERSISTENT => false,
  21. ],
  22. 'separate' => false,
  23. 'distributed' => false,
  24. 'master' => [],
  25. 'slave' => [],
  26. ],
  27. ],
  28. ],
  29. ]);
  30. $container->singleton('option', $option);
  31. $eventDispatch = $this->createMock(IDispatch::class);
  32. $eventDispatch->method('handle')->willReturn(null);
  33. $this->assertNull($eventDispatch->handle('event'));
  34. $container->singleton(IDispatch::class, $eventDispatch);
  35. return $manager;
  36. }

请使用这样的格式来定义连接,系统会自动帮你访问数据库。
系统底层实质上会使用 \Leevel\Option\Option 来管理配置信息。

  1. public function testBaseUse()
  2. {
  3. $manager = $this->createDatabaseManager();
  4. $data = ['name' => 'tom', 'content' => 'I love movie.'];
  5. $this->assertSame('1',
  6. $manager
  7. ->table('guest_book')
  8. ->insert($data)
  9. );
  10. $result = $manager->table('guest_book', 'name,content')
  11. ->where('id', 1)
  12. ->findOne();
  13. $this->assertSame('tom', $result->name);
  14. $this->assertSame('I love movie.', $result->content);
  15. }

数据库主从设置

QueryPHP 允许用户一个主数据库作为写入、更新以及删除,外加多个附属从数据库作为只读数据库来共同提供数据库服务。多个数据库需要需要开启 distributed,而 separate 主要用于读写分离。
master 为主数据库,slave 为附属从数据库设置。

  1. public function testParseDatabaseOptionDistributedIsTrue()
  2. {
  3. $manager = $this->createDatabaseManager();
  4. $option = [
  5. 'driver' => 'mysql',
  6. 'host' => '127.0.0.1',
  7. 'port' => 3306,
  8. 'name' => 'test',
  9. 'user' => 'root',
  10. 'password' => '123456',
  11. 'charset' => 'utf8',
  12. 'options' => [
  13. PDO::ATTR_PERSISTENT => false,
  14. ],
  15. 'separate' => false,
  16. 'distributed' => true,
  17. 'master' => [],
  18. 'slave' => ['host' => '127.0.0.1'],
  19. ];
  20. $optionNew = $this->invokeTestMethod($manager, 'parseDatabaseOption', [$option]);
  21. $data = <<<'eot'
  22. {
  23. "driver": "mysql",
  24. "separate": false,
  25. "distributed": true,
  26. "master": {
  27. "host": "127.0.0.1",
  28. "port": 3306,
  29. "name": "test",
  30. "user": "root",
  31. "password": "123456",
  32. "charset": "utf8",
  33. "options": {
  34. "12": false
  35. }
  36. },
  37. "slave": [
  38. {
  39. "host": "127.0.0.1",
  40. "port": 3306,
  41. "name": "test",
  42. "user": "root",
  43. "password": "123456",
  44. "charset": "utf8",
  45. "options": {
  46. "12": false
  47. }
  48. }
  49. ]
  50. }
  51. eot;
  52. $this->assertSame(
  53. $data,
  54. $this->varJson($optionNew)
  55. );
  56. }

附属从数据库支持二维数组

从数据库支持多个,支持二维数组

  1. public function testParseDatabaseOptionDistributedIsTrueWithTwoDimensionalArray()
  2. {
  3. $manager = $this->createDatabaseManager();
  4. $option = [
  5. 'driver' => 'mysql',
  6. 'host' => '127.0.0.1',
  7. 'port' => 3306,
  8. 'name' => 'test',
  9. 'user' => 'root',
  10. 'password' => '123456',
  11. 'charset' => 'utf8',
  12. 'options' => [
  13. PDO::ATTR_PERSISTENT => false,
  14. ],
  15. 'separate' => false,
  16. 'distributed' => true,
  17. 'master' => [],
  18. 'slave' => [
  19. ['host' => '127.0.0.1'],
  20. ['password' => '123456'],
  21. ],
  22. ];
  23. $optionNew = $this->invokeTestMethod($manager, 'parseDatabaseOption', [$option]);
  24. $data = <<<'eot'
  25. {
  26. "driver": "mysql",
  27. "separate": false,
  28. "distributed": true,
  29. "master": {
  30. "host": "127.0.0.1",
  31. "port": 3306,
  32. "name": "test",
  33. "user": "root",
  34. "password": "123456",
  35. "charset": "utf8",
  36. "options": {
  37. "12": false
  38. }
  39. },
  40. "slave": [
  41. {
  42. "host": "127.0.0.1",
  43. "port": 3306,
  44. "name": "test",
  45. "user": "root",
  46. "password": "123456",
  47. "charset": "utf8",
  48. "options": {
  49. "12": false
  50. }
  51. },
  52. {
  53. "password": "123456",
  54. "host": "127.0.0.1",
  55. "port": 3306,
  56. "name": "test",
  57. "user": "root",
  58. "charset": "utf8",
  59. "options": {
  60. "12": false
  61. }
  62. }
  63. ]
  64. }
  65. eot;
  66. $this->assertSame(
  67. $data,
  68. $this->varJson($optionNew)
  69. );
  70. }

数据库设置只支持数组

数据库主从连接只支持数组。

  1. public function testParseDatabaseOptionMasterAndSlaveMustBeAnArray()
  2. {
  3. $this->expectException(\InvalidArgumentException::class);
  4. $this->expectExceptionMessage(
  5. 'Database option `slave` must be an array.'
  6. );
  7. $manager = $this->createDatabaseManager();
  8. $option = [
  9. 'driver' => 'mysql',
  10. 'host' => '127.0.0.1',
  11. 'port' => 3306,
  12. 'name' => 'test',
  13. 'user' => 'root',
  14. 'password' => '123456',
  15. 'charset' => 'utf8',
  16. 'options' => [
  17. PDO::ATTR_PERSISTENT => false,
  18. ],
  19. 'separate' => false,
  20. 'distributed' => true,
  21. 'master' => [],
  22. 'slave' => 'notarray',
  23. ];
  24. $this->invokeTestMethod($manager, 'parseDatabaseOption', [$option]);
  25. }