3.2. Building the Data Model

Before we can build our blog application, we need to create our blog data model, both in the database and PHP code. We will use postgres in our example app.

Let's create our database, the blog table, and then use Propel to create our PHP object model.

Create a database for our project:

$ createuser -U postgres blog
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE
$ createdb -U postgres blog
CREATE DATABASE
$ psql -U postgres blog -c 'alter database blog owner to blog;'
ALTER DATABASE

Next, create the table for our blog posts. Execute the SQL below in your blog database.

create table blog
(
    blog_id serial,
    post_dts timestamptz,
    title varchar(100) not null,
    post text not null, 
    PRIMARY KEY(blog_id)
);

Now, we tell propel to reverse-engineer our database, build our data model, and update the Propel runtime conf file:

$ cd blog/blog/propel-build
$ propel-gen . creole om convert-conf
Buildfile: /Users/Shared/src/propel-1.3.0beta2/generator/build.xml
[resolvepath] Resolved . to /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/.

propel-project-builder > check-project-or-dir-set:

propel-project-builder > check-project-set:

propel-project-builder > set-project-dir:

propel-project-builder > check-buildprops-exists:

propel-project-builder > check-buildprops-for-propel-gen:

propel-project-builder > check-buildprops:

propel-project-builder > configure:
     [echo] Loading project-specific props from /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./build.properties
 [property] Loading /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./build.properties

propel-project-builder > creole:
    [phing] Calling Buildfile '/Users/Shared/src/propel-1.3.0beta2/generator/build-propel.xml' with target 'creole'
 [property] Loading /Users/Shared/src/propel-1.3.0beta2/generator/./default.properties

propel > creole:
     [echo] +-----------------------------------------------+
     [echo] |                                               |
     [echo] | Generating XML from Creole connection !       |
     [echo] |                                               |
     [echo] +-----------------------------------------------+
[propel-creole-transform] Propel - CreoleToXMLSchema starting
[propel-creole-transform] Your DB settings are:
[propel-creole-transform] driver : (default)
[propel-creole-transform] URL : pgsql://blog:@localhost/blog
[propel-creole-transform] DB connection established
[propel-creole-transform] Processing database
[propel-creole-transform] Processing table: blog
[propel-creole-transform] Writing XML to file: /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./schema.xml
[propel-creole-transform] Propel - CreoleToXMLSchema finished

propel-project-builder > check-project-or-dir-set:

propel-project-builder > check-project-set:

propel-project-builder > set-project-dir:

propel-project-builder > check-buildprops-exists:

propel-project-builder > check-buildprops-for-propel-gen:

propel-project-builder > check-buildprops:

propel-project-builder > configure:
     [echo] Loading project-specific props from /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./build.properties
 [property] Loading /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./build.properties

propel-project-builder > om:
    [phing] Calling Buildfile '/Users/Shared/src/propel-1.3.0beta2/generator/build-propel.xml' with target 'om'
 [property] Loading /Users/Shared/src/propel-1.3.0beta2/generator/./default.properties

propel > check-run-only-on-schema-change:

propel > om-check:

propel > mysqli-check:

propel > om:
     [echo] +------------------------------------------+
     [echo] |                                          |
     [echo] | Generating Peer-based Object Model for   |
     [echo] | YOUR Propel project!                     |
     [echo] |                                          |
     [echo] +------------------------------------------+
[phingcall] Calling Buildfile '/Users/Shared/src/propel-1.3.0beta2/generator/build-propel.xml' with target 'om-template'
 [property] Loading /Users/Shared/src/propel-1.3.0beta2/generator/./default.properties

propel > om-template:
[propel-om] Processing: schema.xml
[propel-om] Processing Datamodel : schema.xml
[propel-om]   - processing database : blog
[propel-om]     + blog
[propel-om]             -> BaseBlogPeer [builder: PHP5ComplexPeerBuilder]
[propel-om]             -> BaseBlog [builder: PHP5ComplexObjectBuilder]
[propel-om]             -> BlogMapBuilder [builder: PHP5MapBuilderBuilder]
[propel-om]             -> (exists) BlogPeer
[propel-om]             -> (exists) Blog

propel-project-builder > check-project-or-dir-set:

propel-project-builder > check-project-set:

propel-project-builder > set-project-dir:

propel-project-builder > check-buildprops-exists:

propel-project-builder > check-buildprops-for-propel-gen:

propel-project-builder > check-buildprops:

propel-project-builder > configure:
     [echo] Loading project-specific props from /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./build.properties
 [property] Loading /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./build.properties

propel-project-builder > convert-conf:
    [phing] Calling Buildfile '/Users/Shared/src/propel-1.3.0beta2/generator/build-propel.xml' with target 'convert-conf'
 [property] Loading /Users/Shared/src/propel-1.3.0beta2/generator/./default.properties

propel > convert-conf:
     [echo] +------------------------------------------+
     [echo] |                                          |
     [echo] | Converting runtime config file to an     |
     [echo] | array dump for improved performance.     |
     [echo] |                                          |
     [echo] +------------------------------------------+
     [echo] Output file: blog-conf.php
     [echo] XMLFile: /Users/alanpinstein/dev/sandbox/blog/blog/propel-build/./runtime-conf.xml
[propel-convert-conf] Processing: schema.xml
[propel-convert-conf] Adding class mapping: BlogMapBuilder => blog/map/BlogMapBuilder.php
[propel-convert-conf] Adding class mapping: BlogPeer => blog/BlogPeer.php
[propel-convert-conf] Adding class mapping: Blog => blog/Blog.php
[propel-convert-conf] Creating PHP runtime conf file: /Users/alanpinstein/dev/sandbox/blog/blog/conf/blog-conf.php

BUILD FINISHED

Total time: 0.7053 seconds

We now have PHP classes to represent our data model, thanks to Propel. Propel automatically puts these classes in blog/blog/classes/blog/*.