Zend_Db_Table Code Generator

I'm working on a small Zend Framework based app for work, and following a discussion in fw-db@lists.zend.com I created a small and crude code generator that connects to a specified DB and produces a series of files, each containing a Zend_Db_Table extending class that represents a table in the DB.

The class will be named after the table, and will already have the $_name and $_primary variables set. Most important - it has $_cols set. $_cols is a protected property of Zend_Db_Table that holds the list of columns of the table. As of ZF 0.6, if $_cols is not set Zend_Db_Table will run a DESCRIBE TABLE SQL query each time you instantiate the table object - a major performance drawback. AFAIK this is not documented in the manual - but setting $_cols prevents this.

This script can be used as a basis for a code generation tool for Zend_Db - especially when the proposed changes will be introduced, and Zend_Db will become more suitable to be used as an MVC model base.

On successful execution, the script will generate a bunch of PHP files in the output directory, each containing a class for each one of your tables. A typical class would look like this:

PHP:
  1. class Qumran_kbArticle extends Zend_Db_Table
  2. {
  3. /**
  4. * Table name
  5. *
  6. * @var string
  7. */
  8. protected $_name = 'kb_article';
  9.  
  10. /**
  11. * Primary key field
  12. *
  13. * @var string
  14. */
  15. protected $_primary = 'id';
  16.  
  17. /**
  18. * List of columns
  19. *
  20. * @var array
  21. */
  22. protected $_cols = array(
  23. 'id'           => 'id',
  24. 'author'       => 'author',
  25. 'title'        => 'title',
  26. 'body'         => 'body',
  27. 'created_at'   => 'createdAt',
  28. 'updated_at'   => 'updatedAt',
  29. 'is_published' => 'isPublished'
  30. );
  31. }

Now, you can take this class and add your own business logic to it, for example:

PHP:
  1. class Qumran_kbArticle extends Zend_Db_Table
  2. {
  3. // ... snip ...
  4. /**
  5. * Get all unpublished articles
  6. *
  7. * @return Zend_Db_Table_Rowset
  8. */
  9. public function getUnpublishedArticles()
  10. {
  11. $where = "is_published = 0";
  12. return $this->fetchAll($where);
  13. }
  14. }

You can get the script here for now, until I'll decide what to do with it. If the Framework team sees it as valuable, it will probably be added to the Framework tree. Usage it quite simple:

CODE:
  1. $ php dbgenerator.php -H

will print out usage information.

I will notify the fw-db mailing list on this script - so if you have any technical comments or you have made some improvements to the script and you want to share them, please reply my post on fw-db@lists.zend.com.

3 Responses to “Zend_Db_Table Code Generator”

  1. Allan Vernon
    January 13th, 2007 | 03:21

    Dear Shahar,
    I tried the script from my windows shell...and I get a fatal error stating

    Fatal error: Call to undefined function getopt() in C:\apache2triad\htdocs\ZBAKS
    T\dbgenerator.php on line 57

    Where is this function defined ?

    Thanks allan vernon

  2. January 13th, 2007 | 09:06

    Hi Allan,

    getopt() is a php built-in function, but apparently it is not implemented on Windows.

    unfortunately, I didn't test my script on Windows - and didn't know getopt() is not defined on Windows until now.

    If a similar tool ever makes it into Zend Framework, we will make sure it is Windows compatible. Until then, I suggest you take a look at the user comments here: http://www.php.net/getopt and see how people are implementing a cross-platform getopt() functionality. You can then patch my script to make it run on Windows. If you do, I'd be happy to see the patch.

  3. Allan Vernon
    January 14th, 2007 | 07:02

    Dear Shahar,
    I tried to submit my patch and other problems. However, it failed to submit but was logged. So if you look at your blog log I presume it will be there.
    Barring that I could send it to your email but I don't want to post it to ZF lists.

    Allan