b-log – betriebsraum weblog

Software Development, Human-Computer Interaction, Projects…

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:

$b.setAuthor($a).

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.

Filed under: Flex/AS3, Software Development

10 Responses to “Killer Combination: AMFPHP, Propel and Creole”

  1. judah says:

    I’ve been looking for something like this. I am interested to hear more about how you are using it.

  2. judah says:

    What happens when you add or delete fields in your database? Does it have to regenerate your class files? Do you end up having a relational number of classes equal to your tables?

    Can you still use this technique if your server is running PHP 4?

    My questions can be summed up in, is there any downsides to this? :)

  3. Kuan says:

    Hey that does sound pretty killer. I developed an app last year with amfphp and a MSSQL db and all the sql writing just about killed me!

    Thanks for the tip

  4. Nek says:

    I’ll be waiting for your article! It’s an interesting topic.

  5. felix says:

    @judah: if you add or delete fields in the database, you have to regenerate the classes but that’s as easy as executing the phing code generator. everything you customized in the subclasses will NOT be overriden.

    Yes, each table is represented by a corresponding php class and a customizable subclass. There’s are a few more classes which contain the main functionality and are extended by the other classes. So if you need to alter a certain table you just include the class you need (and the core class).

    There’s also a php 4 version of propel that isn’t maintained anymore. So you would have to use it at your own risk…

  6. Riccardo Bartoli says:

    Hi, could I use propel and creole without have access to the server?

    I can only access my hosting folder and phpMyAdmin.

    Thanks

  7. Hello! Interesting post. I´m currently using CakePHP together with AMFPHP through the CakeAMFPHP adapter package. CakePHP is very similar in concept to Ruby on Rails and really boost the productivity, I´m really satisfied with it as I can have one knowledge base (business logic on the server) for both HTML and the Flash App.

    Chek it out! http://www.cakephp.org

    Marcelo.

  8. felix says:

    hi marcelo,

    cakephp is also very interesting and i think i’ll use it on one of the next projects…

  9. Glen says:

    Hi,

    I have managed to use AMFPHP with Pear’s DB abstraction layer and also the Pear DB_DataObject class. This latter thing is really nice because once you figure out how to use it, you can build your DB in MySQL, etc. then run a helper class supplied with DB_DataObject to automatically generate your DAO classes. Not only do you get the abstraction, you get automatic building. You can also specify table joining, etc. in a config file. This last bit is slightly tricky, but the tutorials are quite good.
    The final cherry on the cake for me was the fact that you can pass Pear DB recordsets back to Flash from AMF PHP.
    I will check out the Propel / Creole combo to see how it compares, but I would recommend having a look at Pear, unless you are already way ahead on Cake, because the DB_DataObject_FormBuilder class is the icing that lets you do some rapid development with forms in just a few lines of code.

  10. senlab says:

    i am very interested in hearing a full on article of how you do all this. hope you fill us all in on your magic making. :P

Add a comment

You must be logged in to post a comment.