Setup Symfony2 and Postgresql on OS X 10.10 Yosemite? - php

I can't found a way to setup Symfony with a postgresql database on OS X 10.10 (clean install). Here is what I have done:
1) Install PHP 5.6 from Liip (specially built for Symfony): http://php-osx.liip.ch/
2) Install Postgres.app: http://postgresapp.com/
3) Install Symfony: http://symfony.com/
4) Setup my parameters.yml
parameters:
database_driver: pdo_pgsql
database_host: 127.0.0.1
database_port: 5432
database_name: bachelor
database_user: username
database_password: ~
I have created the DB, verified that I have the right PHP cli, the right pgql cli, etc.
I have created a bundle in Symfony and some entities (no errors here, it works with MySQL).
But when I launch any command to interact with the DB like "php app/console doctrine:schema:update --dump-sql", I have this error:
[PDOException]
SQLSTATE[HY000] [2006] MySQL server has gone away
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: PDO::__construct(): MySQL server has gone away
But why? I don't use a MySQL server...
Does someone have any solution?
Thanks!

Can you show us your /app/config/config.yml ? please
Did you change the dbal connection value ?
# Doctrine Configuration
doctrine:
dbal:
default_connection: pgsql
connections:
#Postgresql
pgsql:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8

Related

Create Entity Manager in Symfony3?

I have an existing entity in a Symfony3 project that I am trying to add an entity manager to. My doctrine.yml file looks like this
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
lego:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
auto_mapping: true
default:
connection: default
mappings:
lego:
connection: lego
mappings:
AppBundle: ~
However anytime I try to access this entity manager through php bin/console doctrine:database:create --connection=lego
, it says the manager does not exist! I'm not sure if I'm missing a step - the only thing I've done to create the manager is to make the yml file.
It doesn't show up when I run bin/console debug:container.
Grateful for any help!
1) You are using the same schema name (database name) on the same host in two different entity managers without schema_filters. This means that when you do an update to one of the entity managers it will attempt to delete the data of the other entity manager.
2) You are asking about a entity manager but in the command you are passing a connection.
3) Run php bin/console debug:container | grep doctrine
This will give you all the doctrine services. Update your question accordingly.
4) Provide the exact command and exact error you get so we can track it down to the origin
5) For a more information run the command in verbose mode adding -v at the end
6) Are you importing your doctrine.yml in your config.yml ?

Connection to a db with Doctrine pdo_sqlsrv

I'm trying to connect to my database in SQL Server 2000, but i get an error
could not find driver
when i'm using pdo_sqlsrv.
But if i use sqlsrv i get an error
Attempted to call function "sqlsrv_configure" from namespace
"Doctrine\DBAL\Driver\SQLSrv".
Here is my config.yml
config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
host: "%database_host%"
driver: pdo_mysql
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
connection2:
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
host: "%database_host2%"
driver: pdo_sqlsrv
#driver: sqlsrv i've also tried this
#charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
Does anybody know why i'm getting these errors?
If the function sqlsrv_configure can't be found, then the sqlsrv extension isn't installed. It can be found on the official Microsoft repository here.
You can check if you've installed the extension by running the command php -m in your terminal. Note that if you're running Symfony on a standalone server (eg MAMP, XAMP, or anything else that uses a non-CLI version of PHP) then the extensions used by the CLI and the standalone server may differ.
You can test this by going to the profiler in Symfony (/_profiler by default) and clicking on the "View full PHP configuration" link under the Configuration menu item. This will open the phpinfo() screen. Look for either the sqlsrv or pdo_sqlsrv extensions there.
EDIT
Looking at the Doctrine docs again, it seems that pdo_sqlsrv causes problems. Doctrine recommends using sqlsrv.
pdo_sqlsrv: A Microsoft SQL Server driver that uses pdo_sqlsrv PDO Note that this driver caused problems in our tests. Prefer the sqlsrv driver if possible.
(Emphasis not mine)
Can you try:
driver: pdo_sqlsrv
database_port: 1433
Not sure if that will work, but try it.

symfony pdo_pgsql doctrine:database:create does not use parameters

I use Symfony 3.1.5 and I am trying to run a simple
php bin/console doctrine:database:create
as it is described in the documentation.
My parameters.yml
parameters:
database_host: hhvm_db_1
database_port: 5432
database_name: dbname
database_user: dbuser
database_password: mypassword
My config.yml
doctrine:
dbal:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
I get the following error messages:
[Doctrine\DBAL\Exception\DriverException]
An exception occured in driver: [32681]: could not translate host name "hhvm_db_1
port=5432 dbname=postgres " to address: Name or service not known
[Doctrine\DBAL\Driver\PDOException]
[32681]: could not translate host name "hhvm_db_1 port=5432 dbname=postgres " to a
ddress: Name or service not known
[PDOException]
[32681]: could not translate host name "hhvm_db_1 port=5432 dbname=postgres " to a
ddress: Name or service not known
The error message looks odd to me, because it states dbname=postgres while I have a different database specified in my parameters.yml. I tried to change it directly in the config.yml, but that does not change a thing. Database host and port are taken from my parameters.yml though.
I suppose doctrine is just using default parameters for dbname, as it cannot parse my parameters for some reason. What could be the problem here?
I don't think this is really the issue, but I am running symfony in a docker container with hhvm. hhvm_db_1 in the above error message is the second container running Postgres. When I check the connection with vanilla PHP it works and I can connect to the database. Thus, the host is accessible from my web container.
I found that my system is having problems with the dsn string containing spaces instead of semicolons.
Does not work:
pgsql:host=hhvm_db_1 port=5432 dbname=postgres
Works fine:
pgsql:host=hhvm_db_1;port=5432;dbname=postgres
My initial problem was also solved by that. The DBAL driver for PDOPgSQL is using postgres as the default database to check the connection.

Symfony docker container can't connect to Mysql

My stack is quite common, one container for my Symfony app, another one for Mysql.
For some reason, Symfony can't connect to Mysql container with following error :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
A simple php file to test the MySQL connection, on the Symfony container works with this
<?php
$dbh = new PDO('mysql:host=database;dbname=mysite', 'mysite', 'password');
Here is my Symfony parameters file:
database_driver: pdo_mysql
database_host: database
database_port: 3306
database_name: mysite
database_user: mysite
database_password: password
And here is my docker-compose file :
site:
build: webapp
ports :
- "80:80"
volumes:
- /home/myuser/dev/mysite:/var/www/html/
links:
- database
database:
image: mysql:5.5
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mysite
- MYSQL_USER=mysite
- MYSQL_PASSWORD=password
I also tried with a fresh Symfony project without error.
Remove the database driver and port to use. As you didn't use port forwarding, you don't have to specify the port for MySQL. Also, there's a mistake into your driver, it's not just pdo but pdo_mysql (documentation).
database_host: database
database_name: mysite
database_user: mysite
database_password: password
Also, why do you specify a database_path ? MySQL doesn't need because you call it by TCP/IP. Old SQLite database ?
You could also checkout your config.yml file :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true

Symfony 2 not loading database config file

I'm new to symfony and im following the book with heroku as host, im trying to flush a object to database with doctrine but im getting the following error:
[2015-08-04 05:23:58] request.CRITICAL: Uncaught PHP Exception
PDOException: "SQLSTATE[08006] [7] could not connect to server:
Connection refused Is the server running on host "127.0.0.1" and
accepting TCP/IP connections on port 5432?" at
/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php
line 40 {"exception":"[object] (PDOException(code: 7): SQLSTATE[08006]
[7] could not connect to server: Connection refused\n\tIs the server
running on host \"127.0.0.1\" and accepting\n\tTCP/IP connections on
port 5432? at
/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:40)"}
[]
I got my configuration files working (i guess):
parameters.yml
database_host: host....
database_port: 5432
database_name: ddas1mq8intjqt
database_user: ymmpjzoqbyokbr
database_password: password.....
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: ThisTokenIsNotSoSecretChangeIt
and my config.yml:
doctrine:
dbal:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
I've run the console commands to create the tables from the entity classes and it worked, but creating an object and ->flush seems the problem... don't know if its some server configuration or I've done something wrong.
Thank you
If console command worked, it might be caused by application cache, especially if you run it in prod environment and you have changed database credentials recently. Try to clear the cache for environment you are using (first command for dev, second for prod) and run application again.
php app/console cache:clear
php app/console cache:clear -e prod
I see in error message you used '127.0.0.1', have you tried to type 'localhost' instead? I have similar issue on shared webhosting.

Categories