Zend Framework Database Admin

If you’re looking for a simple tool that uses Zend Framework’s robust database classes (such as Zend_Db and Zend_Db_Table), you can check out zdbform. It’s a short yet effective library that let’s you perform simple administration tasks on your database, with minimal coding.

It’s not a full blown phpMyAdmin, but it’s a simple way to view, edit and add your tables rows in a web interface. Also, don’t expect it to scale, because I am sure this library was written to serve some quick table administration needs, and that it is not ready to handle large datasets. But, it is very convenient if you have a small database to administer.

I implemented it with the Zend MVC components, and following is a brief overview.

In the front controller, or front plugin, or any class that your controller subclasses:

$this->db = Zend_Db::factory('Pdo_Mysql', array(
				'host'     => DB_HOST,
				'username' => DB_USER,
				'password' => DB_PASS,
				'dbname'   => DB_NAME
		));
Zend_Db_Table_Abstract::setDefaultAdapter($this->db);

Then set your controller and view scripts as necessary. Let’s say you have two tables to admin, “clients” and “history”. First make sure they are declared as subclasses of Zend_Db_Table:

require_once "Zend/Db/Table/Abstract.php";

class Clients extends Zend_Db_Table_Abstract
{
    protected $_name = 'clients';
}

class History extends Zend_Db_Table_Abstract
{
    protected $_name = 'history';
}

Your controller would look like:

require_once "Zend/Controller/Action.php";

class AdminController extends Zend_Controller_Action
{

	public function init()
	{
		require_once 'zdbform/zdbform.class.php';
		require_once 'zdbform/zdbform_widgets.class.php';
		require_once 'zdbform/zdbform_validations.php';

		parent::init();

		$this->view->headLink()->appendStylesheet('/zdbform/zdbform.css');
		$this->_helper->viewRenderer('index');
	}

	public function indexAction()
	{
	}

	public function clientsAction()
	{
		$this->view->dbform = new Zdbform('Clients');
		$this->view->dbform->setWidget('description', 'textarea');
		$this->view->dbform->processForms();
	}

	public function historyAction()
	{
		$this->view->dbform = new Zdbform('History');
		$this->view->dbform->processForms();
	}

}

And the single view script you need is admin/index.phtml:

<?php
echo $this->headLink();

include_once "Zend/Filter/Word/CamelCaseToDash.php";
include_once "Zend/Filter/Word/CamelCaseToUnderscore.php";
$cctd = new Zend_Filter_Word_CamelCaseToDash();
$cctu = new Zend_Filter_Word_CamelCaseToUnderscore();
$classes = get_declared_classes();

foreach ($classes as $class)
{
	if (is_subclass_of($class,'Zend_Db_Table_Abstract'))
	{
	?>
		<a href="/admin/<?= strtolower($cctd->filter($class)) ?>"><?= strtolower($cctu->filter($class)) ?></a>&nbsp;&nbsp;
	<?php
	}
}

if ($this->dbform)
{
	?>
	<h1>Table: <?= $this->dbform->tableName ?></h1>
	<?php
	$this->dbform->showForms();
	$this->dbform->showTable();
}
?>

There were also a couple things that needed changing in the zdbform class itself:

  • Replace all PHP_SELF with REQUEST_URL. On the mvc case, PHP_SELF is empty or index.php, and we don’t want all the forms posted there, we want them to go back to /admin/clients or /admin/history
  • After this line
    $this->pk = $tableInfo['primary'];

    I had to add this:

    if (is_array($this->pk))
    	$this->pk = $this->pk[1];
  • zdbform->orderBy is treated as a single column, of you want multiple column sorting you have to hack a bit with getAllRows().

That’s it, point your browser to /admin and you’re good to go. In a very short time and with a little bit of code, you can get something similar to a stripped down version of phpMyAdmin, using the power of Zend Framework.

Upgrading Mootools 1.11 to 1.2 Is Between Hard and Impossible

When it comes to client side, Javascript frameworks are one of the first things that accelerates your development of web applications. Before knowing about the existence of Javascript frameworks, I was coding raw Javascript, in strict functional programming, trying to solve the never ending war of cross-browser compatibility with every new line of code.

And then came along frameworks like Scriptaculous, JQuery, Dojo, and Mootools. These frameworks all tackle the everyday problems of Javascript (cross browser issues being the most important part), build and force an object oriented way of programming Javascript, and create easy to use classes for complicated tasks (like evaluating JSON responses from XmlHttpRequests). On top of that, they add cool and customizable graphics and effects for web UI.

History has its way of determining what framework you eventually use. They are all really similar, and when you’re just in the stage of picking your framework, it usually comes down to the one that has an example in its documentation that is most relevant to your current problem and seems that it solves it with the easiest code. Sometimes though, it’s just a matter of what framework is showcased better, and which has the coolest graphics.

In my case, it was Mootools, and I really can’t remember why. I started using it when it was in version 1.11, and I have been truely happy with it (except for a really nasty bug with https protocol and Internet Explorer). Mootools by version 1.11 was already a robust library, and didn’t require much more development. But of course that open source projects progress and develop, and have to catch up with new browsers and new technologies, and then Mootools released version 1.2.

It’s been already 6 months or so since it was released, and I still haven’t upgraded. And it’s not that I didn’t try — I have tried already 3 times I think — but the upgrade process from 1.11 to 1.2 is probably the hardest upgrade I’ve ever encountered. Version 1.2 is not backward compatible with version 1.11. There are compatibility packages, and several attempts of the community to build on top of these packages, but they never seem to cover all the compatibility needed. There are always a few lines of code you wrote using 1.11, that even with cmpatiblity packages, 1.2 will just break on.

Now don’t get me wrong, I am addicted to Mootools. They say that its core is stable, and they’re right. I just hope that I will never be forced to try again to upgrade to 1.2, or that there will be an easy drop-in way to get code written for 1.11 run on 1.2.