| 
                         我们使用postgres数据库对Web应用程序进行逆向工程.我们想使用symfony框架重构代码,但是我们在使用数据库时遇到了问题. 
目前,我们的项目正在使用MySQL数据库,只是对使用的表使用相同的结构.问题是,现在,我们需要使用postgresql数据库,因为有很多数据,据我们所知,它并没有真正适应MySQL. 
我们进行了大量的研究,但是我们没有成功在symfony项目中创建postgresql数据库. 首先,我们尝试应用本教程:http://www.tutodidacte.com/symfony2-utiliser-une-base-de-donnee-postgresql我们尽可能地为我们的项目调整它. 这是我们的config.yml 
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
#     http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en
framework:
    #esi:             ~
    #translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
         #      http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
        handler_id:  session.handler.native_file
        save_path:       "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets: ~
# Twig Configuration
twig:
     debug:            "%kernel.debug%"
     strict_variables: "%kernel.debug%"
# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
           #Mysql
           default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
           #Postgresql
           pgsql:
                driver:   pdo_pgsql
                host:     localhost
                port:     5432
                dbname:   "%psql_database_name%"
                user:     root
                password: "%psql_database_password%"
                charset:  UTF8
         #mapping_types:
            #geometry: string
    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: "%kernel.debug%"
        #naming_strategy: doctrine.orm.naming_strategy.underscore
        #auto_mapping: true
        entity_managers:
            default:
                connection: default
                 # lister les Bundles utilisant la connexion par defaut
                 #mappings:
                    #monprojetmysqlBundle:  ~
                    #tutoUserBundle:  ~
            pgsql:
                connection: pgsql    # connection name for your additional DB
                # bundles utilisant la connexion Postgresql
                #mappings:
                    # PostgresqlBundle: ~
# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }
 
但是当我们启动这个命令时:php bin / console doctrine:database:create –connection = pgsql我们得到了这个答案: 
  [DoctrineDBALExceptionDriverException]              
    An exception occured in driver: could not find driver  
  [DoctrineDBALDriverPDOException]  
   could not find driver                
  [PDOException]         
   could not find driver  
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 
PHP Warning:  PHP Startup: pdo_pgsql: Unable to initialize module
Module compiled with module API=20131226
PHP    compiled with module API=20121212
These options need to match
in Unknown on line 0
PHP Warning:  PHP Startup: pgsql: Unable to initialize module
Module compiled with module API=20131226
PHP    compiled with module API=20121212
These options need to match
 in Unknown on line 0
  [DoctrineDBALExceptionDriverException]              
  An exception occured in driver: could not find driver  
  [DoctrineDBALDriverPDOException]  
  could not find driver                
  [PDOException]         
   could not find driver  
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”开头的所有文件 
find . -name "php*"
 
根据结果(您可能有很多),删除所有需要删除的文件(此时您的判断是重要的).例如,我删除了/usr/local和/usr/bin中的文件,但没有删除/ Applications或/ Homebrew中的文件. 例子 : 
rm -Rf /usr/bin/php*
rm -Rf /usr/local/php*
 
有时,即使使用sudo,您也可能出现“权限被拒绝”错误,但最终没有出现问题. 
—-重新安装php —- 
一旦删除了关于php的所有内容,您可以使用以下命令行重新安装它: 
brew install php56 --with-postgresql
 
如果您遇到“未找到libz”错误,则需要启动以下命令: 
xcode-select --install
 
并重新启动安装: 
brew reinstall php56 --with-postgresql
 
如果一切顺利,你只需要在php.ini中定义字段date.timezone,你将拥有新的php系统.您可以使用此命令行检查是否已安装pdo_pgsql模块:php -m. 
—-将数据库连接到symfony项目—- 
首先,您需要通过添加以下代码来修改项目中的文件app / config / parameters.yml: 
# Postgresl
    psql_database_driver: pdo_pgsql
    psql_database_host: 127.0.0.1
    psql_database_port: 5432
    psql_database_name: your_database_name
    psql_database_user: your_user_name
    psql_database_password: your_password
 
字段host和port可以不同,但这两个是symfony和postgres数据库的默认值. 
然后,您必须以这种方式在Doctrine Configuration级别修改文件app / config / config.yml: 
# Doctrine Configuration
doctrine:
    dbal:
        default_connection: pgsql
        connections:
           #Mysql
           default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
           #Postgresql
           pgsql:
                driver:   pdo_pgsql
                host:     "%psql_database_host%"
                port:     "%psql_database_port%"
                dbname:   "%psql_database_name%"
                user:     "%psql_database_user%"
                password: "%psql_database_password%"
                charset:  UTF8
        #mapping_types:
            #geometry: string
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
 
这是您可以根据需要调整的示例. 
现在,您可以使用以下命令行将数据库连接到项目: 
php bin/console doctrine:database:create --connection=pgsql
 
如果您的src / AppBundle / Entity中已有实体,则可以使用以下命令创建表: 
php bin/console doctrine:schema:update --force
 
现在一切都好了.我希望,它会帮助那些面临这类问题的人.                         (编辑:莱芜站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |