Killer Combination: AMFPHP, Propel and Creole
January 28, 2006
Have you ever made a database driven (flash) application which made use of lots of sql queries? Then this might interest you. Imagine instead of writing long and complicated sql statements for getting and setting data in your database you could use a simple object oriented syntax. This technique is well-known under the name of Data Access Objects (DAO) or Object Relational Mapping (ORM). More precisely that means: You don’t ever write a single sql statement but use the corresponding classes and objects the orm framework automatically generates for you.
The probably best php orm framework is propel. Propel consists of two main parts: A generator engine and a runtime environment. First you define your whole database in XML (list your tables, attributes and foreign-keys), then let the propel generator do its work (building all sql files, classes and subclasses), include the classes in your php scripts when you need them and use it. So let’s say you’ve defined a table “Book” in the XML definition file. The propel generator creates a class “Book” for you that has all getter and setter methods for storing and retrieving the data. If you wanted to insert a new book with a certain title into the database then in your php file you could write:
$b = new Book(); $b.setTitle("Making things move"); $b.save();
That’s it. Couldn’t be easier. The real power of propel comes in when you have to insert/update/delete data in more than one table (inter-related records). Example: You have an additional table where you store the authors of the books. Create an author:
$a = new Author(); $a.setTitle("Keith Peters")
and set the author object with the book’s setAuthor() method:
Now $b.save() would write two records into the database. Also cool:
You can add parameters in the XML definition file that makes cascading deletes possible (if cascading deletes were enabled, then $b.delete() would also delete the author record). Furthermore propel provides a so-called “Criteria” object which let’s you search all records and iterate over the result objects with a for each loop. Actually you can do almost all sorts of things you could do manually via sql statements and for cases where you want to additionally tweak you can customize the subclasses which won’t get overriden each time by the code generator.
I haven’t talked about the role of Creole and AMFPHP yet. Creole is a database abstraction layer for php 5. It’s required by propel and the purpose of creole is to not bind your application to a certain database. Creole provides a uniform object oriented api so it doesn’t matter if you use a MySQL, MS SQL Server, PostgreSQL, SQLite (or Oracle – not yet fully supported) your code doesn’t have to be changed. If you’re reading this you probably already used the open source flash remoting gateway AMFPHP. This is where everything comes together. It enables you to send complex datatypes seemlessly between the server and the client (flash). And your remoting service class of course uses propel to access the database. Really great :).
It’s quite difficult to explain each framework in detail in a short blog article but if you’re new to the techniques I mentioned do yourself a favor and read through all the docs. For small applications this may all be a little bit overkill but I think on large projects this will save you a lot of time. I’ve already built a small example (nothing exciting, just a database “hello world”) but maybe I’ll write an article on how to setup amfphp, propel, creole (and phing) because it’s a bit involved to get the whole thing running.