Blog

Meet schema.yml

Symfony 2.0 Preview Release

« Back to the Blog

Categories

Feeds

feed Posts feed

comments feed Comments feed

symfony training
Be trained by symfony experts
Sep 07: Online (Nouveautés de 1.3/1.4 - Français)
Sep 22: Paris (Maîtrise de & Doctrine - Français)
Oct 05: Online (Nouveautés de 1.3/1.4 - Français)
Oct 20: Paris (Maîtrise de & Doctrine - Français)
Nov 24: Paris (Maîtrise de & Doctrine - Français)
and more...

Archives

Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.

Until today, Propel integration in symfony implied that your database schema had to be defined in XML files called schema.xml. For instance, the database schema of the first project tutorial looked like this:

<?xml version="1.0" encoding="UTF-8"?>
<database name="propel" defaultIdMethod="native" noxsd="true">
  <table name="weblog_post" phpName="Post">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="title" type="varchar" size="255" />
    <column name="excerpt" type="longvarchar" />
    <column name="body" type="longvarchar" />
    <column name="created_at" type="timestamp" />
  </table>

  <table name="weblog_comment" phpName="Comment">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="post_id" type="integer" required="true" />
      <foreign-key foreignTable="weblog_post">
        <reference local="post_id" foreign="id"/>
      </foreign-key>
    <column name="author" type="varchar" size="255" />
    <column name="email" type="varchar" size="255" />
    <column name="body" type="longvarchar" />
    <column name="created_at" type="timestamp" />
  </table>
</database>

We thought that, for a framework trying to be as simple as possible, it was not the best we could do. As a matter of fact, quite a number of symfony beginners were afraid about its configuration specifically because of this XML file. Some of them were even caught quitting at that point, when they thought that learning a new syntax for schema files was too much for a first try.

So we made it possible to write database schemas in YAML format. You know how we like this format: It's easy to read and write, it's simple to understand, and it's fun. So for instance, the first project tutorial schema (soon to be updated in the doc) can now look like this:

propel:
  weblog_post:
    _attributes: { phpName: Post }
    id:
    title:       varchar(255)
    excerpt:     longvarchar
    body:        longvarchar
    created_at:
  weblog_comment:
    _attributes: { phpName: Comment }
    id:
    post_id:
    author:      varchar(255)
    email:       varchar(255)
    body:        longvarchar
    created_at:

Nothing more. Save this into a schema.yml file in your project config/ folder, then run

$ symfony propel-build-model

...and the Propel model classes are generated just as before.

Of course, to make it simple, we added some magic to it. If you follow the conventions, you don't need to write too much configuration (does that ring a bell?). For instance, if your primary key is called id, then symfony will automatically make it an autoincrement integer. If a field ends with _id, then symfony will recognize a foreign key and enrich the schema as you expect it to. Fields called created_at or updated_at are, of course, of type timestamp, so you don't need to specify it. I18N tables will be recognized as such, as long as their name end with _i18n.

There will soon be a full documentation about it, but until then, we invite you to try this new feature and give us your feedback. It is available since the release 1577 in the SVN trunk.

Comments comments feed

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.