2016-03-16 23 views
5


Siamo retroingegnerizzazione di un'applicazione Web che utilizza un database Postgres. Vogliamo rifattorizzare il codice usando il framework symfony ma stiamo affrontando un problema per utilizzare il database con esso.Utilizzare un database postgres con symfony3


Per il momento il nostro progetto sta lavorando con un database MySQL usando solo la stessa struttura per le tabelle utilizzate. Il problema è che, ora, abbiamo bisogno di usare il database postgresql perché ci sono molti dati e non è adattato a MySQL per quanto ne sappiamo.


Abbiamo fatto molte ricerche ma non siamo riusciti a creare un database postgresql nel progetto symfony.
Per prima cosa, abbiamo provato ad applicare questo tutorial: http://www.tutodidacte.com/symfony2-utiliser-une-base-de-donnee-postgresql abbiamo adattato il più possibile il nostro progetto. Ecco la nostra 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 } 

ma quando abbiamo lanciato questo comando: php bin/console dottrina: database: creare --connection = pgsql abbiamo avuto questa risposta:

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


Sembra che non abbiamo il modulo pdo_pgsql così abbiamo cercato come installarlo.
Per farlo, abbiamo applicato lo script proposto su questa pagina github: https://gist.github.com/doole/8651341#file-install_psql_php-sh
Abbiamo cambiato la versione di postgres.app in 9.5. Dopo alcuni tentativi, siamo finalmente riusciti ad avere pdo_pgsql sul risultato di php -m.
Ma sapere, noi abbiamo questa risposta quando lanciamo il comando: php bin/console dottrina: database: creare --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> 


Abbiamo cercato di fare questo: Install PHP with Postgresql on MAC using homebrew ma didn non cambiare nulla Ora abbiamo 5 PHP avvertimento per pdo_pgsql e pgsql quando lanciamo il comando php bin/console dottrina: database: creare --connection = pgsql


Abbiamo anche visto questo: How to change a database to postgresql with Symfony 2.0? e questo: How to create 2 connections (mysql and postgresql) with Doctrine2 in Symfony2 ma non è stato di grande aiuto perché il primo riguarda debian e stiamo lavorando su OS X El Capitan e il secondo non dice più del tutorial precedente.


Infine, l'unica speranza che abbiamo è che qualcuno possa aiutarci ... Grazie in anticipo.

+0

Cosa dice "ATTENZIONE per pdo_pgsql"? Hai verificato di aver correttamente installato il modulo pdo-pgsql (osservando l'output di 'phpinfo()' - sii consapevole del fatto che php-config potrebbe differire per CLI e Server)? – fateddy

+0

'PHP Warning: PHP Startup: pdo_pgsql: impossibile inizializzare il modulo modulo compilato con il modulo API = 20.131.226 PHP compilato con il modulo API = 20121212 Queste opzioni devono corrispondere in Unknown on line 0 PHP Warning: PHP Startup: pgsql : Impossibile inizializzare il modulo Modulo compilato con API modulo = 20131226 PHP compilato con modulo API = 20121212 Queste opzioni devono corrispondere a in Unknown sulla linea 0' – MarcoD

+1

Questo è sempre lo stesso avviso ma lo abbiamo 5 volte. Ho trovato che con tutto il comando che ho usato, reinstallo php e ora ho 2 php.ini. Proverò a rimuovere tutto completamente e reinstallare php con 'brew install php56 --with-postgresql' – MarcoD

risposta

3


Infine, l'ho risolto rimuovendo tutti i file che gestiscono php e reinstallando php con homebrew.


---- ---- Rimozione php
In primo luogo, ho usato il seguente comando da root (cd /) per trovare tutti i file che iniziano con "php"

find . -name "php*" 


A seconda della risultati (potresti avere molto), rimuovi tutti i file che devono essere rimossi (a questo punto è importante il tuo giudizio). Ad esempio, ho rimosso i file in/usr/local e/usr/bin ma non in/Applicazioni o/Homebrew.
Esempi:

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


A volte, si può avere una "Autorizzazione negata" errore anche con sudo, ma non ha fatto problema alla fine.


---- ---- php reinstallazione


Una volta che tutto ciò che riguarda php è stato rimosso, è possibile reinstallarlo utilizzando la seguente riga di comando:

brew install php56 --with-postgresql 

Se si dispone di un "libz non trovato" l'errore, è necessario lanciare il seguente comando:

xcode-select --install 

e rilanciare l'installazione con:

brew reinstall php56 --with-postgresql 

Se tutto è andato bene, si avrà solo per definire il campo date.timezone in php.ini e si avrà nuovo sistema php. È possibile verificare di avere il modulo pdo_pgsql installato utilizzando questa linea di comando: php -m.


---- Collegare il database per il vostro progetto symfony ----


In primo luogo, è necessario modificare il file app/config/parameters.yml nel progetto aggiungendo il seguente codice:

# 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 

I campi host e porta possono essere diversi ma questi due sono i valori predefiniti per symfony e un database postgres.


Poi, si dovrà modificare il file app/config/config.yml a livello di configurazione Dottrina questo modo:

# 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 

Questo è un esempio che si può adattare come si desidera.

Ora, è possibile collegare il database per il vostro progetto con questa riga di comando:

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

Se si dispone già di entità in src/AppBundle/Entità è possibile creare le tabelle con:

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


Tutto deve andare bene ora. Spero, aiuterà qualcun altro che deve affrontare questo tipo di problemi.