POG is free open source PHP Object Generator. This nifty tool generates MySQL structures and respective PHP classes mapped to it. Despite of obvious CRUD benefits it has pretty flexible API for building up SQL quires. Unless you are trying to avoid any overhead and want to squeeze every CPU cycle out of your server it worthwhile looking at, mainly because it saves enormous amount of time by automating Data Access Objects creation.
We will show you how to plug POG into Zend Framework and use it to access data instead of Zend DB Model.
We assume that you have Zend Framework default setup deployed, up and running.
So you should have the following structure in your site directory:
$ ls -l
drwxr-xr-x 8 user user 4096 2010-04-12 19:39 application
drwxr-xr-x 2 user user 4096 2010-04-08 19:23 docs
drwxr-xr-x 2 user user 4096 2010-04-08 19:23 library
drwxr-xr-x 2 root root 4096 2010-04-08 19:23 logs
drwxr-xr-x 2 user user 4096 2010-04-08 19:23 public
drwxr-xr-x 4 user user 4096 2010-04-08 19:23 tests
just cd to application directory and copy you POG objects folder there. don't forget to set appropriate permissions and directory/files owndership, so your Apache could access it safely. (use CHMOD and CHOWN commands).
$ ls -l
-rw-r--r-- 1 user user 4417 2010-04-12 19:36 Bootstrap.php
drwxr-xr-x 2 user user 4096 2010-04-08 19:23 configs
drwxr-xr-x 2 user user 4096 2010-04-08 19:27 controllers
drwxr-xr-x 2 user user 4096 2010-04-08 19:23 models
drwxr-xr-x 2 user user 4096 2010-04-12 19:38 objects
drwxr-xr-x 4 user user 4096 2010-04-08 19:23 views
All that we have to do now is to modify Bootstrap file to make it read POG objects.
It would need two methods/functions: one to read and include POG objects and another one is to set-up a connection to your database so objects could access (map to) real data.
Here is a function that provides DB connection to POG objects and also sets some basic configuration in GLOBAL array.
{
global $configuration;
$configuration['soap'] = "http://www.phpobjectgenerator.com/services/pog.wsdl";
$configuration['homepage'] = "http://www.mysite.com";
$configuration['revisionNumber'] = "";
$configuration['versionNumber'] = "3.0e";
$configuration['setup_password'] = '';
// to enable automatic data encoding, run setup, go to the manage plugins tab and install the base64 plugin.
// then set db_encoding = 1 below.
// when enabled, db_encoding transparently encodes and decodes data to and from the database without any
// programmatic effort on your part.
$configuration['db_encoding'] = 0;
// edit the information below to match your database settings
$configuration['db'] = 'db_name'; // database name
$configuration['host'] = '192.168.1.10'; // database host
$configuration['user'] = 'db_user'; // database user
$configuration['pass'] = 'db_password'; // database password
$configuration['port'] = '3306'; // database port
//proxy settings - if you are behnd a proxy, change the settings below
$configuration['proxy_host'] = false;
$configuration['proxy_port'] = false;
$configuration['proxy_username'] = false;
$configuration['proxy_password'] = false;
//plugin settings
$configuration['plugins_path'] = APPLICATION_PATH.'/plugins'; //absolute path to plugins folder, e.g c:/mycode/test/plugins or /home/phpobj/public_html/plugins
// document root location for back end
$configuration['be_doc_root_location'] = APPLICATION_PATH;
$this->requireDir('objects');
$configuration['captcha_font'] = 'monofont.ttf'; // default is monofont.ttf , vinque.ttf looks good
}
And here is another function that include POG-objects. Effectively it uses previous function to identify POG objects location and then recursively includes everything that sits in objects directory:
{
$be_doc_root_location = $GLOBALS['configuration']['be_doc_root_location'];
$currDir = getcwd();
chdir($be_doc_root_location);
$dh = @opendir($dir);
if (!$dh)
{
chdir($currDir);
throw new Exception('Cannot open directory '. $dir);
} else {
while (($file = readdir($dh)) !== false)
{
if ($file != '.' && $file != '..')
{
$requiredFile = $dir . DIRECTORY_SEPARATOR . $file;
if ('.php' === substr($file, strlen($file) - 4))
{
require_once $requiredFile;
}
elseif (is_dir($requiredFile))
{
self::requireDir($requiredFile);
}
}
}
closedir($dh);
chdir($currDir);
}
Now you can use POG objects in your controllers like this:
$this->address = $address->GetSingle(array(array('accountid', '=', $this->accounId),
array('statusaddress', 'LIKE', 'ACTIVE')
));
Voila! Jobs done!