Zend Framework Code Generator With Scaffolding – ZFcodo

Update: This project is now hosted at my GitHub account.

While I am a huge fan of Zend Framework, 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 basic forms, controllers, and views. 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 “ZFcodo”, 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 “ZFcodo”. 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 ZFcodo manager file that would set all the constants needed and added the following to /application/configs/application.ini:

; ---
; Database and ZFcodo settings
; ---

zq.db.host = "localhost"
zq.db.username = "sample"
zq.db.password = "sample"
zq.db.name = "sample"

; ZFcodo Model Settins
zq.modelFolder = "/models"
zq.modelBaseFolder = "/Base"

; ZFcodo Form Settings
zq.generateForms = 0
zq.formFolder = "/forms"

; ZFcodo 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 ZFcodo ORM:


protected function _initQcubed()
{
    require APPLICATION_PATH . '/../library/ZFcodo/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/zf-codo.php.

Now I can run zf-codo.php from the command line and it generates my models (including base models), basic controllers, basic views, and basic forms.

ZFcodo Code Generation From Command Line

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:

Zend Framework Code Generator

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. ZFcodo 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: Zend Framework ORM
Update: This project is now hosted at my GitHub account
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.

7 Responses to “Zend Framework Code Generator With Scaffolding – ZFcodo”

  • marcosdsanchez

    Very nice article. I myself wanted to do the same thing a while back. Unfortunately I didn’t have the time.
    It’s a good idea, but you should not forget the power that the QForms have. QForms are very powerful and very reusable.

    Just as a comment: why did you choose the name Zcodo? Zcodo was the project that QCubed was based on (which was based on Qcodo and is now dead). Why not choose another name like ZendCodo or ZCubed or something like that :) … just to avoid confusions…

    Thanks

  • Clint

    Thanks Marcos. I love Qforms too, but correct me if I am wrong, the Qform is essentially the controller in the MVC paradigm of the Qcubed framework, and the Qform template is essentially the view. I wanted to do things the Zend way as much as possible, so I generated Zend Controllers and Zend Forms instead.

    I didn’t know that a Zcodo project already existed, so perhaps I will have to rename it. :-) Zcubed is a good option.

  • Vartan

    Hi Clint,

    Not a substantive comment, but I thought I should mention it anyway: while your project is using QCubed, in your blog post, the word qcodo is linkified almost every time it’s mentioned, while the word QCubed is never a link (other than the “go here …”. Considering that QCubed lacks much needed marketing and the way Search Engines rank external links (especially coming from blogs), it would be nice and helpful to QCubed, if you could make QCubed links also.

    Thanks. Now I can go back to actually reading your post :-)

  • Clint

    Updated for the sake of Qcubed. It’s the least I can do. Although linking to a site with the company name is hardly as useful as doing this: Best PHP Framework
    The anchor text makes all the difference. :-)

  • Alex

    Hi, i just downloaded the project and i like it so far. I will give you a bigger feedback after i implement a project with it. Maybe i can help you with the development

  • cyanure

    hi
    i tried to use your application, and php leave an exception “segmentation fault” … any idea ?

Leave a Reply