使用symfony3的postgres数据库 [英] Use a postgres database with symfony3

查看:436
本文介绍了使用symfony3的postgres数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们正在使用postgres数据库对Web应用进行改造。我们想使用symfony框架来重构代码,但是我们面临着使用数据库的问题。




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



<我们进行了大量的研究,但是我们没有在symfony项目中创建一个postgresql数据库。

首先,我们尝试应用本教程: http://www.tutodidacte.com/symfony2-utiliser-une-base-de-donnee-postgresql 我们为我们的项目尽可能地进行了调整。
这是我们的 config.yml

  import:
- {resource :parameters.yml}
- {resource:security.yml}
- {resource:services.yml}

#将参数放在每个不需要更改的位置应用程序部署的机器
#http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
参数:
语言环境:en

框架:
#esi:〜
#translator:{fallbacks:[%locale%]}
secret:%secret%
router:
资源:%kernel.root_dir%/ config / routing.yml
strict_requirements:〜
form:〜
csrf_protection:〜
验证:{enable_annotations:true
#serializer:{enable_annotations:true}
模板:
引擎:['twig']
default_locale:%locale%
trusted_hosts:〜
信托d_proxies:〜
会话:
#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%
片段:〜
http_method_override:true
assets:〜

#Twig配置
twig:
debug:%kernel.debug%
strict_variables:%kernel.debug%

#Doctrine配置
原则:
dbal:
default_connection:默认
连接:
#Mysql
默认值:
驱动程序:pdo_mysql
主机: %database_host%
port:%database_port%
dbname:%database_name%
user:%database_user%
password:%database_password%
字符集:UTF8
#Postgresql
pgsql:
驱动程序:pdo_pgsql
主机:localhost
端口:5432
dbname:%psql_database_name%
用户:root
密码:%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:
默认值:
连接:默认

#lister les Bundles utilisant la connexion par defaut

#mappings:
#monprojetmysqlBundle:〜
#tutoUserBundle:〜

pgsql:
连接:pgsql#连接名称为您的额外的DB

#bundles utilisant la connexion Postgresql
#mappings:
#PostgresqlBundle:〜

#Swiftmailer配置
swiftmailer:
transport:%mailer_transport%
主机:%mailer_host%
用户名:%mailer_user%
密码: %mailer_password%
spool:{type:memory}

但是当我们启动这个命令: php bin / console doctrine:database:create --connection = pgsql 我们有这个答案:

  [Doctrine\DBAL\Exception\DriverException] 
驱动程序出现异常:找不到驱动程序



[Doctrine\DBAL\ Driver \ PDOException]
找不到驱动



[PDOException]
无法nd driver


doctrine:database:create [--connection [CONNECTION]] [--if-not-exist] [-h | --help] [-q | - [-v | vv | vvv | --verbose] [-V | --version] [--ansi] [--no-ansi] [-n | --no-interaction] [-e | - env ENV] [--no-debug] [ - ]< command>


似乎我们没有模块 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警告:PHP启动:pdo_pgsql:无法初始化模块
使用模块编译的模块API = 20131226
使用模块API编译的PHP = 20121212
这些选项需要匹配
在未知的行0
PHP警告:PHP启动:pgsql:无法初始化模块
使用模块编译的模块API = 20131226
PHP编译与模块API = 20121212
这些选项需要匹配
在未知的行0


[Doctrine\DBAL\Exception\DriverException]
司机出现异常:找不到驱动程序



[Doctrine\DBAL\Driver\PDOException]
找不到驱动



[PDOException]
找不到驱动


doctrine:database:create [ - 连接[CONNECTION]] [ - 不存在] [-h | --help] [-q | --quiet] [-v | vv | vvv | --verbose] [-V | - 版本] [--ansi] [--no-ansi] [-n | --no-interaction] [-e | --env ENV] [--no-debug] [ - ]< command>


我们尝试这样做:使用自制软件在Mac上安装Postgresql PHP PHP
但是没有改变任何东西。现在,当我们启动命令 php bin / console doctrine:database:create -connection = pgsql


$ b时,我们为pdo_pgsql和pgsql提供了5个PHP警告$ b


我们也看到了这一点:$ b​​ $ b
如何使用Symfony 2.0将数据库更改为postgresql?,这样:如何在Symfony2中使用Doctrine2创建2个连接(mysql和postgresql),但是并没有真正的帮助,因为第一个关心debian和我们正在从事OS X El Capitan工作,第二个并不比以前的教程更多。




最后,我们唯一的希望是有人可以帮助我们...
提前谢谢。

解决方案


最后,我通过删除所有管理php和的文件用自制软件重新安装php。




----删除php ----

首先,我从根目录使用了以下命令( cd / )来查找所有以php开头的文件

  find。 -namephp *


根据结果(您可能会有很多)删除所有需要删除的文件(在这一点上,这是你的判断)。例如,我删除了/ 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 not found错误,你将需要启动以下命令:

  xcode-select --install 
/ pre>

并重新启动安装:

  brew重新安装php56 --with-postgresql 

如果一切顺利,您只需要定义字段 date.timezone 在php.ini中,您将拥有新的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_name
psql_database_password:your_password

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




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

 #Doctrine配置
doctrine:
dbal:
default_connection:pgsql
connections:
#Mysql
默认值:
驱动程序:pdo_mysql
主机:%database_host%
端口:%database_port%
dbname:%database_name%
用户:%database_user%
密码:%database_password%
charset:UTF8
#Postgresql
pgsql:
驱动程序:pdo_pgsql
主机: %psql_database_host
port:%psql_database_port
dbname:%psql_database_name%
用户:%psql_database_user
密码:%psql_data base_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 



现在一切都行。我希望,这将有助于面对这种问题的其他人。



We are retro-engineering a web application using a postgres database. We want to refactor the code using the symfony framework but we are facing a problem to use the database with it.


For the moment our project is working with a MySQL database just using the same structure for the used tables. The problem is that, now, we need to use the postgresql database because there is a lot of data and it's not really adapted to MySQL as far as we know.


We made a lot of research but we didn't succeed to create a postgresql database in the symfony project.
First, we tried to apply this tutorial : http://www.tutodidacte.com/symfony2-utiliser-une-base-de-donnee-postgresql we adapted it as much as possible for our project. Here is our 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 }

but when we launched this command : php bin/console doctrine:database:create --connection=pgsql we had this answer :

  [Doctrine\DBAL\Exception\DriverException]              
    An exception occured in driver: could not find driver  



  [Doctrine\DBAL\Driver\PDOException]  
   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] [--] <command>


It seems that we don't have the module pdo_pgsql so we searched how to install it.
To do it, we applied the script proposed on this github page : https://gist.github.com/doole/8651341#file-install_psql_php-sh
We changed the version of postgres.app to 9.5. After a few tries, we finally succeeded to have pdo_pgsql on the result of php -m.
But know, we have this answer when we launch the command : 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


  [Doctrine\DBAL\Exception\DriverException]              
  An exception occured in driver: could not find driver  



  [Doctrine\DBAL\Driver\PDOException]  
  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] [--] <command>


We tried to do this : Install PHP with Postgresql on MAC using homebrew but it didn't change anything. Now we have 5 PHP WARNING for pdo_pgsql and pgsql when we launch the command php bin/console doctrine:database:create --connection=pgsql


We have also seen this : How to change a database to postgresql with Symfony 2.0? and this : How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2 but it didn't really help because the first one concern debian and we are working on OS X El Capitan and the second one don't tell more than the previous tutorial.


Finally, the only hope we have is that someone can help us... Thank you in advance.

解决方案


Finally, I fixed it by removing all the files that manage php and reinstalling php with homebrew.


---- Removing php ----
First, I used the following command from root (cd /) to find all the files starting by "php"

find . -name "php*"


Depending on the results (you might have a lot), remove all the files that need to be removed (at this point it's your judgement that matter). For example, I removed files in /usr/local and /usr/bin but not in /Applications or /Homebrew.
Examples :

rm -Rf /usr/bin/php*
rm -Rf /usr/local/php*


Sometimes, you can have a "permission denied" error even with sudo but it didn't make problem at the end.


---- Reinstalling php ----


Once everything concerning php is removed, you can reinstall it using the following command line :

brew install php56 --with-postgresql

If you have a "libz not found" error, you will need to launch the following command :

xcode-select --install

and relaunch the installation with :

brew reinstall php56 --with-postgresql

If everything went well, you will only have to define the field date.timezone in php.ini and you will have new php system. You can check that you have the pdo_pgsql module installed using this commande line : php -m.


---- Connect your database to your symfony project ----


First, you need to modify the file app/config/parameters.yml in your project by adding the following code :

# 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_name
    psql_database_password: your_password

The fields host and port can be different but this two are the default values for symfony and a postgres database.


Then, you will have to modify the file app/config/config.yml at the Doctrine Configuration level this way :

# 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

This is an example you can adapt as you wish.

Now, you can connect your database to your project with this command line :

php bin/console doctrine:database:create --connection=pgsql

If you already have entities in your src/AppBundle/Entity you can create your tables with :

php bin/console doctrine:schema:update --force


Everything must be alright now. I hope, it will help someone else who faces this kind of problems.

这篇关于使用symfony3的postgres数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆