[geeklog-devel] Geeklog 2 and Propel

Tony Bibbs tony at tonybibbs.com
Wed Nov 3 18:10:14 EST 2004


If you do anything today please read this entire message...it's a bit 
long but I want to be sure you all get a quick look of something that 
really excites me and could make all our PHP5 coding faster.

I just finished my test drive with Propel.  I had to do so because we 
are looking for an Object Relational Mapping (ORM) tool to use in our 
PHP environment here at work.  I do have it working already with a copy 
of the Auth_Enterprise database I have.  As a recap here is what it can do:

1) You can define your data model and use Propel to generate an XML file 
that describes your database.  Here is a snippet from that file (note 
there is a DTD you can use for validating this):

<table name="ae_group">
    <column name="grp_id" type="SMALLINT" required="true" 
primaryKey="true"/>
    <column name="grp_app_id" type="VARCHAR" size="30" required="true" 
primaryKey="true" default=""/>
    <column name="grp_logical_name" type="VARCHAR" size="50" 
required="true" default=""/>
    <column name="grp_display_name" type="VARCHAR" size="50" 
required="true" default=""/>
    <column name="grp_descr" type="VARCHAR" size="255" required="true" 
default=""/>
    <column name="grp_create_date" type="INTEGER" required="true" 
default="0"/>
    <column name="grp_change_date" type="INTEGER"/>
    <column name="grp_change_user_name" type="VARCHAR" size="75" 
primaryKey="true"/>
    <foreign-key foreignTable="ae_application">
      <reference local="grp_app_id" foreign="app_id"/>
    </foreign-key>
    <foreign-key foreignTable="ae_user">
      <reference local="grp_change_user_name" foreign="user_name"/>
    </foreign-key>
  </table>

If you look closely you'll notice it basically is the schema represented 
in XML.  Only gotcha was that I had to define all the foreign keys by 
hand even though this database uses foreign key.  Not a huge deal.

2) Then you can build your project.  A build seems like something that 
PHP projects shouldn't have to do but here is what happens during the build:
- Schema is used to build php5 classes, two for each table in the 
database.  The first is the model class and it is an object the 
represents the table with all the appropriate getters and setters.  The 
second is a 'Peer' class which is used to do searches against that table
- The config file (ini style) is then converted to a PHP array

3) You simply copy the classes and the config file over to your webtree 
and you have at it.  Here is code to save a new application in 
Auth_Enterprise:

include_once 'propel/Propel.php';
Propel::init('/var/www/localhost/htdocs/PropelCreoleTest/Auth_Enterprise-conf.php');
include_once 'PropelCreoleTest/classes/Auth_Enterprise/AeApplication.php';
$app = new AeApplication();
$app->setAppId('PROPEL_TEST_APP_ID');
$app->setAppName('Propel Auth_Enterprise Test Application');
$app->setAppContactEmail('tony at geeklog.net');
$app->save(); // note this is transaction safe.

Only gotcha is the build system is required on developer machines.  The 
build tool is called Phing (http://phing.info).  It seems to be 
complicated to me yet because I don't understand how it works really but 
using Propel most of the Phing specifics are hidden from you.  Note that 
Phing is not required when deploying applications (i.e. end-users 
wouldn't need it).

This may all seem complicated but after I got Phing, Propel and Creole 
installed (most of which use the pear install command) I was able to get 
this code up and running in a matter of minutes.

Anyway, even if it doesn't make it into Geeklog 2 it should at least be 
considered.  The main reason is that by using Propel, we don't worry 
about the database access portion anymore an we can simply concentrate 
on implementing our features.

Only gotchas so far is that it requires the use of their DB abstraction 
layer called Creole (i.e. it won't work with PEAR::DB).

--Tony



More information about the geeklog-devel mailing list