簡體   English   中英

使用帶有symfony3的postgres數據庫

[英]Use a postgres database with symfony3


我們使用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我們得到了這個答案:

  [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>


似乎我們沒有模塊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


  [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>


我們嘗試這樣做: 使用自制軟件在MAC上使用Postgresql安裝PHP,但它沒有改變任何東西。 現在,當我們啟動命令php bin / console doctrine時,我們為pdo_pgsql和pgsql提供了5個PHP警告:database:create --connection = pgsql


我們也看到過: 如何使用Symfony 2.0將數據庫更改為postgresql? 這個: 如何在Symfony2中使用Doctrine2創建2個連接(mysql和postgresql),但它並沒有真正幫助,因為第一個關注debian而我們正在開發OS X El Capitan而第二個關注的不僅僅是上一個教程。


最后,我們唯一的希望是有人可以幫助我們......提前謝謝你。


最后,我通過刪除管理php的所有文件並使用自制軟件重新安裝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


現在一切都好了。 我希望,它會幫助那些面臨這類問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM