The definitive guide of Symfony 1.2

8.5. Database Connections

The data model is independent from the database used, but you will definitely use a database. The minimum information required by symfony to send requests to the project database is the name, the credentials, and the type of database. These connection settings can be configured by passing a data source name (DSN) to the configure:database task:

> php symfony configure:database "mysql://login:passwd@localhost/blog"

The connection settings are environment-dependent. You can define distinct settings for the prod, dev, and test environments, or any other environment in your application by using the env option:

> php symfony  --env=prod configure:database "mysql://login:passwd@localhost/blog"

This configuration can also be overridden per application. For instance, you can use this approach to have different security policies for a front-end and a back-end application, and define several database users with different privileges in your database to handle this:

> php symfony  --app=frontend configure:database "mysql://login:passwd@localhost/blog"

For each environment, you can define many connections. Each connection refers to a schema being labeled with the same name. The default connection name used is propel and it refers to the propel schema in Listing 8-3. The name option allows you to create another connection:

> php symfony  --name=main configure:database "mysql://login:passwd@localhost/blog"

You can also enter these connection settings manually in the databases.yml file located in the config/ directory. Listing 8-17 shows an example of such a file and Listing 8-18 shows the same example with the extended notation.

Listing 8-17 - Shorthand Database Connection Settings

all:
  propel:
    class:          sfPropelDatabase
    param:
      dsn:          mysql://login:passwd@localhost/blog

Listing 8-18 - Sample Database Connection Settings, in myproject/config/databases.yml

prod:
  propel:
    param:
      hostspec:           mydataserver
      username:           myusername
      password:           xxxxxxxxxx

all:
  propel:
    class:                sfPropelDatabase
    param:
      phptype:            mysql     # Database vendor
      hostspec:           localhost
      database:           blog
      username:           login
      password:           passwd
      port:               80
      encoding:           utf8      # Default charset for table creation
      persistent:         true      # Use persistent connections

The permitted values of the phptype parameter are the ones of the database systems supported by PDO:

  • mysql
  • mssql
  • pgsql
  • sqlite
  • oracle

hostspec, database, username, and password are the usual database connection settings.

To override the configuration per application, you need to edit an application-specific file, such as apps/frontend/config/databases.yml.

If you use a SQLite database, the hostspec parameter must be set to the path of the database file. For instance, if you keep your blog database in data/blog.db, the databases.yml file will look like Listing 8-19.

Listing 8-19 - Database Connection Settings for SQLite Use a File Path As Host

all:
  propel:
    class:      sfPropelDatabase
    param:
      phptype:  sqlite
      database: %SF_DATA_DIR%/blog.db