The definitive guide of Symfony 1.0

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 access codes, and the type of database. These connection settings should be entered in the databases.yml file located in the config/ directory. Listing 8-17 shows an example of such a file.

Listing 8-17 - 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 connection settings are environment-dependent. You can define distinct settings for the prod, dev, and test environments, or any other environment in your application. This configuration can also be overridden per application, by setting different values in an application-specific file, such as in apps/myapp/config/databases.yml. 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.

For each environment, you can define many connections. Each connection refers to a schema being labeled with the same name. In the example in Listing 8-17, the propel connection refers to the propel schema in Listing 8-3.

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

  • mysql
  • mssql
  • pgsql
  • sqlite
  • oracle

hostspec, database, username, and password are the usual database connection settings. They can also be written in a shorter way as a data source name (DSN). Listing 8-18 is equivalent to the all: section of Listing 8-17.

Listing 8-18 - Shorthand Database Connection Settings

all:
  propel:
    class:          sfPropelDatabase
    param:
      dsn:          mysql://login:[email protected]/blog

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