Symfony2 DBAL & ORM Setup
So I have been fiddling around with Symfony2 all morning and read the main documentation or at least half of it. I am stuck at anything related to Database.
My simple question is: do we make the database structure before hand or not?
Documentation says make the Entity class and then generate the database table using database:create on CLI. I followed and made a blog entity class with orm annotations.
ran the command:
php app/console doctrine:database:create
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Applications/MAMP/htdocs/flairbagSy2/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnec开发者_如何学Gotion.php on line 36
Could not create database for connection named blog
SQLSTATE[HY000] [2002] No such file or directory
I think this has something to do with the location of the mysql socket file but I don't know how to change the path of the socket file in Symfony2's configuration.
If anyone could just point out where do I change the path of socket file.
I once had a similar problem with CakePHP and the simple fix was to add a port key to the db connection array:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'cake',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock',
);
How do I do that in Symfony2.
The doctrine:database:create command will not populate the structure of the tables but rather create the database on your database manager like MySQL. To populate the database structure, use the doctrine:schema:create
command for the initial creation of the structure and the doctrine:schema:update --force
command when you want to update the structure after you made some modifications to the annotations.
As you have mentionned, you need to configure some parameters in Symfony2 for Doctrine to work correctly. Those parameters need to be set in the config.yml configuration file that you can find in the app/config folder of the standard Symfony2 distribution. Here's a sample of the configuration you need to add to the config.yml file:
# Doctrine Services Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3396
dbname: dbname_dev
user: dbname_dev
password: yourpassword
logging: "%kernel.debug%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
mappings:
AcmeBundle: ~
The first part of the configuration, the dbal part, is used configure your database connection information. I would suggest you to first test the connection with the root account of your database and then when everything is working well, change the settings to use a dedicated user. This way, you are sure that you don't have some privileges problems or other kind of problems that could be related to the dedicated user.
The second part, the orm part, is use to configure the mapping of your entities to the database. This part is required if you want Symfony2 to populate the database structure automatically. Replace AcmeBundle with the name of your bundle in the sample above to enable the orm functionalities in your project.
On a side note, the Symfony2 team is working on simplifying the configuration and I think there is now an easier way to configure the Doctrine parameters. The sample I propose you is the one I am using right now with the Beta1 of Symfony2.
And finally to answer your question, I think the best is to let Symfony2 deals with the creation and update the database structure. One argument in favor of this would be that you don't need to maintain an SQL file dealing with the database structure. This information is implicitely held by the annotations in your entity so you just have to change you annotations and call the doctrine:schema:update --force
command to update the structure. With this in mind, you should not create the structure before hand but let Symfony2 create it for you.
Here's the workflow to test that everything is working:
- Update your config.yml (in app/config) to include Doctrine configuration parameters
- Open a prompt and go to the root folder of the Symfony distribution (where you see the app, src, vendor and web folders)
- Run
php app/console doctrine:database:create
to create the database in MySQL - Run
php app/console doctrine:schema:create
to create the database structure - Check in MySQL that the database and the structure have been created correctly
Hope this help.
Regards,
Matt
The original question asked about how to define a non-standard socket in the Symfony2 configuration.
The way I do it is to add a new config setting in app/config/config.yml that pulls in the value from the project parameters file (app/config/parameters.ini).
In app/config/parameters.ini, add a database_socket value:
;[parameters.ini]
database_socket = "/var/mysql/mysql.sock"
and then in app/config/config.yml, pull in the parameter value:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
unix_socket: %database_socket%
charset: UTF8
That will pass the custom socket path into doctrine's connection.
If using MAMP You can create a symbolic link in your /var/mysql/ folder to /Applications/MAMP/tmp/mysql/mysql.sock file
First check to see if your /var/mysql folder exists if it doesnt make it using:
mkdir /var/mysql
Go to the above folder then write the following to make your symbolic link
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
you might also have timezone issues (not related to the above problem but if you're a mamp user you will probably have this one as well) which you can fix by putting the following in your /private/etc/php.ini file (note: this is not your php.ini file found in your MAMP folder)
date.timezone = "America/Montreal"
Note: you might need to change your actual timezone which may not be the same as mine.
References: http://www.iamseree.com/application-development/doctrine-error-on-mac-osx-it-is-not-safe-to-rely-on-the-systems-timezone-settings-you-are-required-to-use-the-date-timezone-setting-or-the-date_default_timezone_set-function
http://mikecroteau.wordpress.com/2011/08/07/symfony2-could-not-create-database-for-connection-named-sqlstatehy000-2002-no-such-file-or-directory/
精彩评论