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.



