Zend Framework Code Generator With Scaffolding – Zcodo
I am still relatively new to Zend Framework, and while I think it is awesome, I miss having the code generation that is bundled with other frameworks I use. Since my favorite codegen/ORM is from the Qcubed project, I decided to take the Qcubed code generator and customize it for Zend Framework.
A little background on Qcubed: Qcubed is a framework branched off of the Qcodo project. Qcubed code generation connects to your already built database and generates your models, views, and controllers based on your database schema. Qcubed has a built-in ORM that uses the Active Record model and also a custom querying language similar to Doctrine ORM. (Go here for more information on the Qcubed project)
The advantage to using Qcubed over Doctrine is that I have it generating not only my models, but also all my forms, and soon I will have it generating an entire scaffolding structure with controllers and all. I will also be running some benchmark tests because I think that out of the box, Qcubed ORM performs better than Doctrine. I have dubbed this project "Zcodo", which references the origins of the Qcubed project: Qcodo. So far I have a hacked together proof of concept that I think is interesting. Here is what I have with some example code so you can see why this will save you time setting up your Zend Framework Projects.
To get this going, I started with a basic Zend Framework application structure. Then I copied the entire Qcubed project into my custom library folder "Zcodo". I stripped out anything I could see from Qcubed that was specifically for the framework, and not related to the ORM or code generation and altered some files a little to match a Zend Framework application. I then created a Zcodo manager file that would set all the constants needed and added the following to /application/configs/application.ini:
; --- ; Database and Zcodo settings ; --- zq.db.host = "localhost" zq.db.username = "sample" zq.db.password = "sample" zq.db.name = "sample" ; Zcodo Model Settins zq.modelFolder = "/models" zq.modelBaseFolder = "/Base" ; Zcodo Form Settings zq.generateForms = 0 zq.formFolder = "/forms" ; Zcodo Controller Settings zq.generateControllers = 0 zq.controllerFolder = "/controllers"
My next step was to add an init function to my /application/Bootstrap.php file that initialized the Zcodo ORM:
protected function _initQcubed()
{
require APPLICATION_PATH . '/../library/Zcodo/Codegen/Manager.php';
$zqConfig = $this->getOption('zq');
$manager = new Zcodo_Manager($zqConfig);
$manager->loadOrm();
}
And finally, I modified the Qcubed code generation PHP script to work from a CLI and put it into /application/scripts/zcodo.php.
Now I can run zcodo.php from the command line and it generates my models (including base models), basic controllers, basic views, and basic forms.

For my example, I created a simple "customer" table with "first name" and "last name". The code generator added the following files for the table:
application/controllers/CustomerController.php application/forms/Customer.php application/models/Customer.php application/models/Base/Customer.php application/views/scripts/customer/list.phtml application/views/scripts/customer/create.phtml application/views/scripts/customer/edit.phtml
Then to list the customers, all I do is go to http://projectlocation/customer/list and I see this page:

The codegen alone can get you off to a great start on your next ZF project, but with the built-in ORM, you can save hours on your project. Zcodo will turn the 80/20 rule into the 90/10 rule...
Right now this is more of a proof of concept (a nice way of saying it is a hack job) but I will continue to work on this, and if there seems to be a demand for this, I will work on it even more. I have implemented the ORM into a few live projects already and it seems to be working great.
You can get the entire project source code here: zcodo_full-project.zip
To use it, just edit the application.ini file with your database info, and then run the script application/scripts/zcodo.php from the CLI.
That easy...
The future plans for this project are to:
- organize the directory structure the Zend way
- add Zend_Paginator ability to the ORM
- style the generated forms
- and allow module based codegen
Let me know if you have any questions.
Custom Fonts with Google WebFont Loader
I have recently been working on adding local fonts to my customized version of svg-edit. The idea is to have a font selector with several fonts from my local system. I have been using the recently released Google WebFont Loader API to load font files from my local server into the website. Google has made it easier than ever. (note: Google will collect stats from your website if you use this API, just like they do if you use any of their services.)
The first thing I needed was font files. Not all browsers are alike when using the css @font-face elements. Paul Irish educated me on that one. So I needed different versions of the font file based on the browser. Luckily, Font Squirrel has a Font Face Generator which generates all the necessary files and the style sheet to boot. I thought to myself: "Dang, that was easier than I thought it would be. I think I'll donate some money to Font Squirrel for developing that awesome app." Then I didn't get around to it... maybe tomorrow.
Once you have all the font files and style sheet up on your server, you can instantiate them with the Google WebFont Loader. Why not just include the style sheet? For a couple of reasons: 1) You can't get rid of FOUT 2) If you are loading multiple fonts (like in a font selector) it would take a long time to load them all up-front (Some of the files are 50k+). For my font selector, I want to be able to load the fonts when the font is selected, then it will be cached on the user's computer for all future requests.
To keep track of which fonts were loaded (so as not to load them again with WebFont) I created an array with all the font definitions for my selector (Only three to start). I then created the Smm.loadFont function to load the font. The function takes 3 arguments: 1) The font to load 2) [optional] The callback for when the font has finished loading, and 3) [optional] the callback for WHILE the font is loading.
var Smm = {
fontDirectory: 'css/fonts/',
imageDirectory: 'css/fonts/font-images/'
};
Smm.Fonts = {
BrushScript: {
cssFile: 'BrushScript.css',
imageFile: 'brush-script.png',
loadType: 'custom',
loaded: false
},
CloisterBlackLight: {
cssFile: 'CloisterBlackLight.css',
imageFile: 'cloister.png',
loadType: 'custom',
loaded: false
},
Cooper: {
cssFile: 'Cooper.css',
imageFile: 'cooper.png',
loadType: 'custom',
loaded: false
}
};
Smm.loadFont = function(fontFace, active, loading){
console.log('Loading font: ' + fontFace);
if(Smm.Fonts[fontFace]['loaded'] === false){
Smm.Fonts[fontFace]['loaded'] = true;
WebFont.load({
custom: {
families: [fontFace],
urls: [Smm.fontDirectory+Smm.Fonts[fontFace]['cssFile']]
},
loading: loading,
active: active
});
}
else {
active();
}
};
I added the imageFile setting to each font so that a picture would be used instead of text. This allows the user to see a preview of the font without downloading it. The last thing I did, was create the HTML for the font loader.
You can check out the demo HERE
UPDATE: I integrated the drop-down into svg-edit. Check it out: HERE
Custom Error Messages on Zend Form Validators
Zend Form is extremely powerful, and I love most of the built in validators. But some of the validators are overkill for many projects. Take the EmailAddress validator for instance. I have never worked on a web-form where I wanted 3 error messages to appear if the Email address entered was invalid. (To see what I mean, just type in "a@a" for your email address and see what Zend_Validate_EmailAddress displays). I have seen several questions and complaints about this problem (Example 1 or Example 2) and thought I would offer up my fix.
I have found that the quickest way to to control your error messages with the EmailAddress Validator is to create your own email validator that extends from Zend_Validate_EmailAddress, and then override the isValid function. Here is the shortest version I have come up with:
class Clint_Validate_EmailAddress extends Zend_Validate_EmailAddress
{
public function isValid($value)
{
$response = parent::isValid($value);
if(!$response){
$this->_messages =
array(self::INVALID => "Please enter a valid email address");
}
return $response;
}
}
This class simply calls the parent function isValid(), and if it returns false, it sets the _messages array to have only one error message of your choice. Call it a hack if you want, but it works, and it works without putting logic in the controller which means I can re-use this form wherever I want. As always, let me know if you have a different and/or better way of doing this.
Installing Magento on OS X
Mark Hopwood has created a great post on installing Magento stand-alone on OS X. I know there have been several posts in the Magento forums with questions on this. Just remember if you are installing Magento on OS X with Zend Server, you need to open your TCP/IP sockets in the mysql.conf file so that Mysql can be connected to using localhost or the local IP address.
Zend Server CE on OS X
I finally did it. I bought a mac. And after navigating around OS X for ten minutes using the multi-touch pad, I can honestly tell you I don't think I'll ever go back. As I use it more I am curious to see if developing on my new, sleek, aluminum toy will continue to bring the feelings of ecstasy.
The first thing I did was install Zend Server CE to develop and test locally on my machine. Here are my notes on the Zend Server CE installation for the OS X, which didn't seem as user friendly as it's Windows Installer counterpart. But I am such a noob with Macs it is probably just me. This is for all you windows developers making the switch.
