Zend_Tool and Model generation

Zend_Tool is a command line tool that helps you setup a Zend Framework MVC application. You can also use it after you have set up your application to add controllers, actions, views, configuration and models. Since I use Zend Studio for development, I am used to the integrated version of Zend_Tool. This means that if you set up a Zend Framework MVC application as a new Zend Studio project, you will have menus available that let you add things like controllers and actions.

This is really nice of course, but the nature of the tool, that is command-line by birth, makes that I would like to be able to use it from the command-line too. Also, if I am able to use it from the commandline, I can write batch scripts, to execute multiple Zend_Tool commands in one go, for example to build the skeleton for an entire module. For this post I will assume that you are able to find out how to make Zend_Tool (the zf command) available on the command-line, if you do not know that, you can find an explanation in the Zend Framework manual online.

What I am interested in mostly is code generation for models. Models are objects that represent business logic. In my projects, models usually map to a database table, more or less accurately. I usually call them DataObjects or Entities because this is a better description, since they implement the Active Record pattern. I know that many developers consider the Active Record pattern to be a bad practice, because it is usually tightly coupled with the database backend. This post however is not about that discussion. There are ways to use something that looks very much like Active Record but that allows to replace the database implementation relatively easy. One of these ways is to generate base classes. If you want to replace the base classes, you can simply generate new ones, that have a different implementation when it comes to the database. The child classes that derive from the base classes can stay the same mostly, because their extensions should be database agnostic. That aside, I am interested in what Zend_Tool can do for model or entity generation. There is also DbTable generation, which is possibly closer to Active Record than model generation.

For now, the reason I started this post is that I really do not know what to expect if I use Zend_Tool to generate models or DbTables. So let’s try!

First of all, I need to create a project and a database configuration. Without that, it is not possible to do DbTable class generation. This is because DbTable classes belong to a project and describe data.

Creating a project

$ ./zf.sh create project trytool

The output is surprising, since I do not usually type commands first time right:

Creating project at /Users/bartmcleod/Documents/werk/trytool
Note: This command created a web project, for more information setting up your VHOST, please see docs/README
Testing Note: PHPUnit was not found in your include_path, therefore no testing actions will be created.

The downside is that no test are generated, because PHPUnit was not found on the include path. That’s a real pitty, because one the things I was really curious about is if tests would also be auto generated.

Now it is time to point my local MAMP installation at the newly created project, to see if I get a ready made webapplication.

And yes!

This is the default application happy screen:

Zend Framework default application welcome screen

Creating a model

From what I read in the manual it is possible to create models now. That means models will have nothing to do with the database yet. I supect  that they will be nothing more than stubb classes on which we can build. I must now change the working directory to inside my project, otherwise Zend_Tool will not know where to put the models.

$ cd trytool
$ ../zf.sh create model user

And this is the result:

$ ../zf.sh create model user
Note: The canonical model name that is used with other providers is "User"; not "user" as supplied
Creating a model at /Users/bartmcleod/Documents/werk/trytool/application/models/User.php
Updating project profile '/Users/bartmcleod/Documents/werk/trytool/.zfproject.xml'

Indeed, the User.php file contains a very basic class, without an implementation, but it’s a startingpoint.

Creating a DbAdapter configuration

It’s not really hard to configure a database in the default configuration file located at configs/application.ini. The interesting part here is to create it using Zend_Tool. The syntax is easy, it is url encoded.

$ ../zf.sh configure dbadapter "adapter=Pdo_Mysql&user=root&password=root&dbname=trytool"

Again I look in amazement at the result:

A db configuration for the production section has been written to the application config file.

This tool seems to work really well! This is the result in the config file:

resources.db.adapter = "Pdo_Mysql"
resources.db.params.host = "127.0.0.1"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.dbname = "trytool"

Now I do not really have a database name trytool, so if I want to try creating DbTable classes, I will need to create that database and load some tables in there. To keep it simple, I will load my WordPress database in there. I have a dump of that already downloaded. With that in place, I will try to create a DbTable class for the trytool.wp-options table.

Creating a DbTable object

$ ../zf.sh create dbtable WpOptions wp_options

This is the result:

Creating a DbTable at /Users/bartmcleod/Documents/werk/trytool/application/models/DbTable/WpOptions.php
Updating project profile '/Users/bartmcleod/Documents/werk/trytool/.zfproject.xml'

Now what really interests me is how intelligent the created DbTable class will be. I suspect it will only have a $_name property set to ‘wp_options’ and extend Zend_Db_Table_Abstract. And this is exactly true:

<?php

class Application_Model_DbTable_WpOptions extends Zend_Db_Table_Abstract
{
    protected $_name = 'wp_options';
}

What I was hoping for is quite different, I hoped it would create Doctrine entities. I don’t know where I got that idea, but will have to wait till another post. It is possible to add your own providers to Zend_Tool, maybe Doctrine entity generation is supported by a adding a different provider.

DbTable objects for a whole database at once

There is also a command by which you can create DbTable objects for your entire database. There are some restrictions however, and the command seems to contain a typo. The syntax of the command is:

$ ../zf.sh create db-table.from-database

At first I thought from-database was a variable you  would have to replace with the name of your database, but it isn’t. You have to type ‘from-database’ literally, which means you can only do this for a single database you have configured. I suppose there is no support for a multidb configuration. You should also note the hyphen (‘-‘) in db-table as opposed to dbtable in the command for a single table.

Author: Bart McLeod

Zend Framework MVC specialist. Also interested in PHP, Zend Framework in general, Three.js and VRML.

Leave a Reply

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