Peter Robins, his website

Setting up this site in Symfony

Phase 4: adding db-based pages

The default Symfony structure is based on the principle of each project having its own database/model. However, most things in Symfony can be configured, so we'll alter the default structure to correspond better to my website, which has evolved over the last 10 years or so, and where each section of the site has its own database, some of which are stored as SQLite files, some in MySQL databases.

So, how to configure having several different dbs? The databases are defined in 3 configuration files in config/:

  1. databases.yml: this is a simple list by environment (this is 'all' by default, as defined in the first line). For the blog we used the default database, which is called 'propel'. All that is needed for further dbs is add them to the end of this file; for example:
      ballads:
        class:          sfPropelDatabase
        param:
          dsn: mysql:dbname=ballads;host=localhost 
          username: xxx
          password: yyy
          encoding: utf8
          persistent: true
          pooling: false
          classname: PropelPDO
    defines a database called 'ballads' using a MySQL db called 'ballads'. To make things clearer, we'll rename the blog db from 'propel' to 'blog'
  2. schema.yml: this is used when generating the model and assigns tables/model objects to one of the databases listed in databases.yml; the peer classes generated assign a constant DATABASE_NAME, for example, const DATABASE_NAME = 'ballads';. Again, the default is 'propel', and in the existing file the blog tables/objects are grouped under this. Propel only allows one db per schema file, but you can have any number of files, so long as their filename ends with schema.yml. So we can rename the existing one from schema.yml to blog-schema.yml (also editing the database name to 'blog' so it matches the entry we just made in databases.yml). For the other databases, we simply create further schema files as appropriate, for example, ballads-schema.yml for the ballads tables/objects
  3. propel.ini: connection settings are also defined in this file, but this is only used by the propel:build-sql and propel:build-schema tasks. As these are probably not used very much, and not at all in the production environment, this isn't too important. If you have several dbs and want to run one of these functions, edit propel.ini as appropriate

Now we can move on to the particular apps:

February 2008, updated April 2010