Zend Framework and Rapid Application Development with PHP
Posted by Andre Liem
* This blog post is part of Invoke’s participation in the 2008 Vancouver Blogathon for Charity
Why use the Zend Framework?
In the past few years, web development has become a very competitive arena for programming languages, frameworks, libraries and IDE’s. As a result, there has been a plethora of options for programmers to choose from. In most cases, a significant amount of time can be spent on searching for tools to speed up development instead of recreating from scratch. This article is aimed at php developers who are looking to learn more about how not to reinvent the wheel, in particular, with the new Zend framework. This is not a comparative list of all php frameworks, as there are many good ones out there, CakePHP and Symphony to name two. I’ve evaluated other frameworks to some degree, not exhaustively by any means, and the Zend framework seems to fit nicely with my requirements. It may not for you, but I’ll try to persuade you in this article :).
A little about the Zend Framework
The Zend framework is only a few years old and as of this article has reached version 1.6 Release Candidate 1. In some ways, Zend is very similar to the PEAR framework, but not quite as large and for lack of a better word, more “web 2.0” focused. So what does that mean? For one, it supports Model View Controller (MVC), caching, form generation, configuration management, JSON parsing, DOJO (AJAX) support, mailing, several web services API integrations, and more. The downside of a new framework is completeness and community. Some of these objects do very little (yet), and your best source of help is yourself or googling old articles about previous versions of Zend.
Aside from this, the Zend framework offers power and flexibility. You can choose what objects you want to use, and it’s very easy to integrate Zend into other frameworks. Given this alone, as a php developer you should learn Zend because it’s just as useful as PEAR. I’ve already noticed some articles popping up about how to use Zend objects within other frameworks like CakePHP. The flexibility of Zend is my favourite feature, as I’ll show later it’s very easy to integrate other tools into the framework.
Coding Faster… Rapid Application Development
So how can you achieve Rapid Application Development (RAD) with Zend? Well, when I use the term RAD, I may not be falling inline with the traditional definition but I’m referring to the idea of coding less. Perhaps I should just say three words, Ruby on Rails. If you don’t know what Ruby on Rails is then read this article
You may ask why not just use Ruby on Rails or CakePHP? My primary reason is again flexibilty, it’s very easy to pick and chose the features you want to use.
Tools you’ll need and what they do
Download and install these tools:
- fabFORCE visual db designer
http://www.fabforce.net/dbdesigner4/
Download the free visual db designer from fabFORCE.net. This tool allows you to design your database visually. - Zend Framework
http://framework.zend.com/
I recommend downloading the stable release (1.5) and not the RC1, unless you are just exploring the framework. - Propel
http://propel.tigris.org/
You’ll need to download creole and phing. Setting up Propel is the hardest part. - Convert fabFORCE XML to Propel XML
http://blog.tooleshed.com/docs/dbd2propel/transform.php
- Eclipse PHP IDE (PHP version)
http://www.eclipse.org/pdt/
The Development Process Flow
Here are the steps. Make sure you’ve installed all the applications and followed their documentation.
Part A – Generating the database classes without coding.
This part will show you how to generate database objects using db designer.
- Design the database in fabFORCE’s visual db designer. Very nice and easy to use.
- Save db designs to native fabFORCE XML format.
- Save db designs to SQL create script for mysql.
- Convert fabFORCE XML (from step 2) to propel’s native XML format. Use this conversion script http://blog.tooleshed.com/docs/dbd2propel/transform.php. *
- Create the database with the sql script (from step 3).
- Run propel on the converted XML from step 4.
*Unfortunately, this script doesn’t fill in the foreign key rows, so you’ll have to manually update the XML outputted from this script. Regardless, this tool is still a major time saver.
At this point, you now have a set of classes that represent your database layer, or your CRUD layer (CREATE, READ, UPDATE, DELETE). Oh, and I forgot, you also have your database setup too.
Part B – Setting up Zend and Eclipse
- Create the directories for a Zend MVC framework (learn here). You’ll have two areas, one for the application MVC code and another for the web root.
- Startup eclipse with a new workspace.
- Create a new project in eclipse that points to the “application” directory
(the one with the MVC folders). - Create a new project in eclipse that points to the “web root” directory.
(the one with the index.php and htaccess file) - Create a new project that points to your Zend library folder.
- Create a new project that points to your propel folder.
- Add project references to the application project from step 3:
a. Right-click project -> Properties
b. Select PHP Include Path
c. Select Projects tab
d. Click Add
e. Select Zend and Propel. - Build all of the projects (Project -> Build All)
At this point, your environment is almost setup.
Part C – Programming
- Copy the generated propel objects (from part A - step 6) into the “models” folder of the MVC.
- Build the project again.
- Setup the index.php bootstrap file to include appropriate libraries. **
- You’re done!
**This part can be a bit tricky so here’s some sample code below that would fit at the top of your index.php bootstrap file.
set_include_path('.' . PATH_SEPARATOR .
'../library/' . PATH_SEPARATOR .
'../application/models/' . PATH_SEPARATOR .
'../application/config/' . PATH_SEPARATOR .
get_include_path());
//In the above:
//../application refers to the relative path to your MVC folders.
//../library would contain Zend and Propel
require_once 'propel/Propel.php'; //generated db objects
/*
* include all propel models here
*/
//This is where you include classes generated by propel
require_once 'databasemodel/Person.php';
require_once 'databasemodel/PersonPeer.php';
With this model setup, you can code CRUD actions like this:
Example 1 – Controller – Create a record (in controller)
$person = new Person();
$person->setUsername("myusername");
$person->setAge("18");
$person->save();
Example 2 – Controller – Read records
$c = new Criteria(); $people = PersonPeer::doSelect($c); $this->view->users = $users;
Example 2b- View - Echo records
<?php foreach($this->users as $currentUser): echo $this->currentUser->getUsername(); endforeach; ?>
Example 3 – Controller – Update records
$c = new Criteria(); $currentUserId = $this->_request->getParam(“userId”); $c->add(PersonPeer::ID, $currentUserId); $currentPerson = PersonPeer::doSelectOne($c); $currentPerson->setAge(“19”); $currentPerson->save();
Example 4 – Controller – Delete records
$c = new Criteria(); $currentUserId = $this->_request->getParam(“userId”); $c->add(PersonPeer::ID, $currentUserId); $currentPerson->delete();
In the examples above, eclipse would provide intellisense auto completion if you pressed ctrl + after typing $person->. Aside from making coding faster, this reduces error because you’re only using methods that actually exist. Eclipse does this by compiling the included libraries when you build the projects.
You may also notice that I’ve shown the basic CRUD (Create, Read, Update, Delete) use cases that Ruby on Rails and CakePHP make so easy. Another framework, Symphony, actually uses propel too so you can often rely on Symphony’s community for help when it comes to making database calls with Propel.
The first steps may seem like a lot to do, but it’s really not that complicated and the time saved in the future is well worth it. You may wonder why I didn’t use Zend’s Zend_DB Object? I really prefer Propel DB because it removes the need to code any SQL and it generates code from XML.
To reiterate the most important part of this article, you have a lot of flexibility with this setup, you can pick and choose what aspects you want to follow. From here you could easily integrate more Zend objects without having to worry about the model layer.
Further thoughts
The one area I did not cover, and which is a major bonus for frameworks like CakePHP and Ruby On Rails, is AJAX support. As of this article, Zend has just shipped with DOJO support and they will continue to provide more in the coming months. There is not enough room to cover DOJO, but my initial experience with DOJO is very positive. All I can add is that it seems like it will be easy to integrate DOJO as a fairly independant AJAX layer that doesn’t become tangled in your PHP code.






August 4th, 2008 at 5:59 pm
This is another reason why I would remain a Ninja Web Developer. I just wrote a comic post on Rapid Applications Development. Thanks for the hint on Propel, the name sounds new to me I will look it up.
Nice one.
August 5th, 2008 at 9:06 pm
I’ve tried out your guide. I may be wrong here, and if I am, please forgive me, but the main problem I face when developing with closed packages like cake or rails is when I have a major change in the model layer.
Let me give you a basic example:
A user has an address attributed to him, then after some time, it is decided that he must now be able to have (N) multiple addresses.
To do that, the best practice would be to separate the address from the user, and link it via an ID (relationally - is there such a word?).
With you method, I am still stuck having to re-write model files manually o regenerating all the model files using propel, in turn, having to redo most of my work (at least related to the address).
Even though I have not given Zend FW it’s full proper test drive, I’ve had extensive interaction with CodeIgniter (over hte last few weeks), and I found it’s learning curve to be quite low.
With a tiny bit of tweaking here and there of the basic downloaded framework (check out http://wiki.bugz.com.br) you could easily create a site in a flash, and still keeping it fully “extendable”.
You might want to test drive CI a bit. (not that I am an evangelist or anything)
August 6th, 2008 at 3:00 am
Nice post
Just a quick note: it’s Symfony, not Symphony.
August 6th, 2008 at 9:57 am
Hey guys,
Thanks for the feedback…
Greg: You’re right that it can be a pain when the model changes, and it does… but it’s not that hard. For a recent project, my model/db changed about 10 times in a week and recompiling then copying over wasn’t that time consuming. You can extend the model classes with classes that should never be overwritten so you’re not redoing model layer work.
As for the controller level, yes… you’ll have to do some refactoring but if you’re using eclipse and do a build you’ll find the errors quick. Still better than using plain old SQL, but I haven’t tried CI but have read a bit about it… if you’re doing a ton of model changing constantly then it’s just bad planning. Either way I know that my method isn’t the best by any means, nor most efficient… but it is pretty efficient and you can pick and choose what you want to use. I just like flexibility when I program, I don’t like having to be stuck with one particular set of frameworks.
January 1st, 2009 at 7:34 pm
it is Symfony, not Symphony