Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/PDOConnection.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.

like image 785
Aayush Avatar asked May 01 '11 13:05

Aayush


2 Answers

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:

  1. Update your config.yml (in app/config) to include Doctrine configuration parameters
  2. Open a prompt and go to the root folder of the Symfony distribution (where you see the app, src, vendor and web folders)
  3. Run php app/console doctrine:database:create to create the database in MySQL
  4. Run php app/console doctrine:schema:create to create the database structure
  5. Check in MySQL that the database and the structure have been created correctly

Hope this help.

Regards,
Matt

like image 63
Matt Avatar answered Sep 28 '22 23:09

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.

like image 22
stereoscott Avatar answered Sep 28 '22 23:09

stereoscott