php – 使用带有symfony3的postgres数据库

前端之家收集整理的这篇文章主要介绍了php – 使用带有symfony3的postgres数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们使用postgres数据库对Web应用程序进行逆向工程.我们想使用symfony框架重构代码,但是我们在使用数据库时遇到了问题.

 目前,我们的项目正在使用MySQL数据库,只是对使用的表使用相同的结构.问题是,现在,我们需要使用postgresql数据库,因为有很多数据,据我们所知,它并没有真正适应MysqL.

我们进行了大量的研究,但是我们没有成功在symfony项目中创建postgresql数据库.
首先,我们尝试应用本教程:http://www.tutodidacte.com/symfony2-utiliser-une-base-de-donnee-postgresql我们尽可能地为我们的项目调整它.
这是我们的config.yml

  1. imports:
  2. - { resource: parameters.yml }
  3. - { resource: security.yml }
  4. - { resource: services.yml }
  5. # Put parameters here that don't need to change on each machine where the app is deployed
  6. # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
  7. parameters:
  8. locale: en
  9. framework:
  10. #esi: ~
  11. #translator: { fallbacks: ["%locale%"] }
  12. secret: "%secret%"
  13. router:
  14. resource: "%kernel.root_dir%/config/routing.yml"
  15. strict_requirements: ~
  16. form: ~
  17. csrf_protection: ~
  18. validation: { enable_annotations: true }
  19. #serializer: { enable_annotations: true }
  20. templating:
  21. engines: ['twig']
  22. default_locale: "%locale%"
  23. trusted_hosts: ~
  24. trusted_proxies: ~
  25. session:
  26. # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
  27. handler_id: session.handler.native_file
  28. save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
  29. fragments: ~
  30. http_method_override: true
  31. assets: ~
  32. # Twig Configuration
  33. twig:
  34. debug: "%kernel.debug%"
  35. strict_variables: "%kernel.debug%"
  36. # Doctrine Configuration
  37. doctrine:
  38. dbal:
  39. default_connection: default
  40. connections:
  41. #MysqL
  42. default:
  43. driver: pdo_MysqL
  44. host: "%database_host%"
  45. port: "%database_port%"
  46. dbname: "%database_name%"
  47. user: "%database_user%"
  48. password: "%database_password%"
  49. charset: UTF8
  50. #Postgresql
  51. pgsql:
  52. driver: pdo_pgsql
  53. host: localhost
  54. port: 5432
  55. dbname: "%psql_database_name%"
  56. user: root
  57. password: "%psql_database_password%"
  58. charset: UTF8
  59. #mapping_types:
  60. #geometry: string
  61. orm:
  62. default_entity_manager: default
  63. auto_generate_proxy_classes: "%kernel.debug%"
  64. #naming_strategy: doctrine.orm.naming_strategy.underscore
  65. #auto_mapping: true
  66. entity_managers:
  67. default:
  68. connection: default
  69. # lister les Bundles utilisant la connexion par defaut
  70. #mappings:
  71. #monprojetMysqLBundle: ~
  72. #tutoUserBundle: ~
  73. pgsql:
  74. connection: pgsql # connection name for your additional DB
  75. # bundles utilisant la connexion Postgresql
  76. #mappings:
  77. # PostgresqlBundle: ~
  78. # Swiftmailer Configuration
  79. swiftmailer:
  80. transport: "%mailer_transport%"
  81. host: "%mailer_host%"
  82. username: "%mailer_user%"
  83. password: "%mailer_password%"
  84. spool: { type: memory }

但是当我们启动这个命令时:PHP bin / console doctrine:database:create –connection = pgsql我们得到了这个答案:

  1. [Doctrine\DBAL\Exception\DriverException]
  2. An exception occured in driver: could not find driver
  3. [Doctrine\DBAL\Driver\PDOException]
  4. could not find driver
  5. [PDOException]
  6. could not find driver
  7. doctrine:database:create [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--]

 似乎我们没有模块pdo_pgsql所以我们搜索了如何安装它.
 为此,我们应用了此github页面上提出的脚本:https://gist.github.com/doole/8651341#file-install_psql_php-sh
 我们将postgres.app的版本更改为9.5.
经过几次尝试,我们终于成功地在PHP -m的结果上有了pdo_pgsql.
 但是知道,当我们启动命令时,我们有这个答案:PHP bin / console doctrine:database:create –connection = pgsql

  1. PHP Warning: PHP Startup: pdo_pgsql: Unable to initialize module
  2. Module compiled with module API=20131226
  3. PHP compiled with module API=20121212
  4. These options need to match
  5. in Unknown on line 0
  6. PHP Warning: PHP Startup: pgsql: Unable to initialize module
  7. Module compiled with module API=20131226
  8. PHP compiled with module API=20121212
  9. These options need to match
  10. in Unknown on line 0
  11. [Doctrine\DBAL\Exception\DriverException]
  12. An exception occured in driver: could not find driver
  13. [Doctrine\DBAL\Driver\PDOException]
  14. could not find driver
  15. [PDOException]
  16. could not find driver
  17. doctrine:database:create [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--]

 我们试图这样做:Install PHP with Postgresql on MAC using homebrew
但它没有改变任何东西.现在,当我们启动命令PHP bin / console doctrine时,我们为pdo_pgsql和pgsql提供了5个PHP警告:database:create –connection = pgsql

我们也看到了这个:
How to change a database to postgresql with Symfony 2.0?和这个:How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2但它并没有真正帮助,因为第一个关注debian,我们正在开发OS X El Capitan,而第二个关注的不仅仅是前一个教程.

 最后,我们唯一的希望是有人可以帮助我们……
先感谢您.

最佳答案
最后,我通过删除管理PHP的所有文件并用homebrew重新安装PHP来修复它.

 —-删除PHP —-
 首先,我使用root(cd /)中的以下命令查找以“PHP”开头的所有文件

  1. find . -name "PHP*"

 根据结果​​(您可能有很多),删除所有需要删除文件(此时您的判断是重要的).例如,我删除了/usr/local和/usr/bin中的文件,但没有删除/ Applications或/ Homebrew中的文件.
 例子 :

  1. rm -Rf /usr/bin/PHP*
  2. rm -Rf /usr/local/PHP*

 有时,即使使用sudo,您也可能出现“权限被拒绝”错误,但最终没有出现问题.

 —-重新安装PHP —-

 一旦删除了关于PHP的所有内容,您可以使用以下命令行重新安装它:

  1. brew install PHP56 --with-postgresql

如果您遇到“未找到libz”错误,则需要启动以下命令:

  1. xcode-select --install

并重新启动安装:

  1. brew reinstall PHP56 --with-postgresql

如果一切顺利,你只需要在PHP.ini中定义字段date.timezone,你将拥有新的PHP系统.您可以使用此命令行检查是否已安装pdo_pgsql模块:PHP -m.

 —-将数据库连接到symfony项目—-

 首先,您需要通过添加以下代码修改项目中的文件app / config / parameters.yml:

  1. # Postgresl
  2. psql_database_driver: pdo_pgsql
  3. psql_database_host: 127.0.0.1
  4. psql_database_port: 5432
  5. psql_database_name: your_database_name
  6. psql_database_user: your_user_name
  7. psql_database_password: your_password

字段host和port可以不同,但​​这两个是symfony和postgres数据库的默认值.

 然后,您必须以这种方式在Doctrine Configuration级别修改文件app / config / config.yml:

  1. # Doctrine Configuration
  2. doctrine:
  3. dbal:
  4. default_connection: pgsql
  5. connections:
  6. #MysqL
  7. default:
  8. driver: pdo_MysqL
  9. host: "%database_host%"
  10. port: "%database_port%"
  11. dbname: "%database_name%"
  12. user: "%database_user%"
  13. password: "%database_password%"
  14. charset: UTF8
  15. #Postgresql
  16. pgsql:
  17. driver: pdo_pgsql
  18. host: "%psql_database_host%"
  19. port: "%psql_database_port%"
  20. dbname: "%psql_database_name%"
  21. user: "%psql_database_user%"
  22. password: "%psql_database_password%"
  23. charset: UTF8
  24. #mapping_types:
  25. #geometry: string
  26. orm:
  27. auto_generate_proxy_classes: "%kernel.debug%"
  28. naming_strategy: doctrine.orm.naming_strategy.underscore
  29. auto_mapping: true

这是您可以根据需要调整的示例.

现在,您可以使用以下命令行将数据库连接到项目:

  1. PHP bin/console doctrine:database:create --connection=pgsql

如果您的src / AppBundle / Entity中已有实体,则可以使用以下命令创建表:

  1. PHP bin/console doctrine:schema:update --force

现在一切都好了.我希望,它会帮助那些面临这类问题的人.

猜你在找的MySQL相关文章