Clint Berry

Full-stack Web Developer

Wannabe Entrepreneur

Zend Framework Code Generator With Scaffolding – ZFcodo

Posted on 05 Aug 2010 in Zcodo, Zend Framework | 12 comments

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.

The following two tabs change content below.
I am a full-stack web developer that is passionate about start-ups.

12 comments

  1. marcosdsanchez / August 5th, 2010 13:49

    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

  2. Clint / August 5th, 2010 16:25

    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.

  3. Vartan / August 5th, 2010 20:42

    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 :-)

  4. Clint / August 5th, 2010 21:05

    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. :-)

  5. Alex / December 4th, 2010 13:25

    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

  6. Clint / December 4th, 2010 19:38

    Alex,

    I’d love to have you help. I just uploaded all the most recent code to GitHub, so feel free to fork it and I’d be happy to merge your changes in.

    https://github.com/clintberry/zf-codo

  7. cyanure / May 4th, 2011 9:20

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

  8. david / November 14th, 2012 13:28

    Hello I manage to create the classes.But I don’t can access the page “customer/list” .

    This page return erros in my class: ‘Model_Base_Customer’ not found
    and the libraries also ZFcodo.

    Maybe my error can be, my application.ini is don’t loading

    Any tips?

    Follows my application.ini

    [production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    phpSettings.date.timezone = “Europe/London”
    includePaths.library = APPLICATION_PATH “/../library”
    includePaths.models = APPLICATION_PATH “/models”
    includePaths.forms = APPLICATION_PATH “/forms”
    includePaths.images = APPLICATION_PATH “/images”
    bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
    bootstrap.class = “Bootstrap”
    appnamespace = “Application”
    resources.frontController.controllerDirectory = APPLICATION_PATH “/controllers”

    resources.frontController.moduleDirectory = APPLICATION_PATH “/models”

    phpSettings.date.timezone = “UTC”
    resources.layout.layoutpath = APPLICATION_PATH “/layouts”
    resources.frontController.throwExceptions = 1

    zq.db.host = “localhost”
    zq.db.username = “root”
    zq.db.password = “”
    zq.db.name = “customer”

    ; ZFcodo Model Settings
    zq.modelFolder = “APPLICATION_PATH /models”
    zq.modelBaseFolder = “APPLICATION_PATH /models/Base”

    ; ZFcodo Form Settings
    zq.formFolder = “\forms”

    ; ZFcodo Controller Settings
    ;zq.controllerFolder = “\controllers”
    ;zq.viewsBase = “\views\scripts”

  9. Clint / November 14th, 2012 16:32

    @David, this library is out of date and out of touch. I was bullish about it when I started, but now I have moved more to the backboneJS single page application structure. Sorry about that.

  10. Vinicius Barreto / January 10th, 2013 19:04

    Great job Clint!

    I’ve just read the article and I’m downloading it now. Just a question: are you still working on that? On github the last upload was 2 years ago.

  11. Clint / January 10th, 2013 19:33

    @Vincius – Sorry, I am not actively working on it at all, and it is in a poor state of affairs. I currently work at a python/node.js shop so my PHP side projects have dwindled.

  12. Scottux / September 24th, 2013 18:04

    Good job with this. Sad that I just now found this article. I have been working lately to move the Codegen portion of QCubed out into its own Composer package. I have a plan to generate REST APIs quickly and easily while abstracting as much of QCubed away as possible. Also I had plans to codegen javascript files since I do a lot of Angular/Backbone/Ember work as well. The QForm related classes are awesome, but I don’t always want them in my project or if I want to use something instead of jQuery UI for my controls it is difficult.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>