One possible refactoring is to move some of the common code from the RowDataGateway implementations into a super class.
For example:
PHP Code:
<?php
class PearRowDataGateway {
var $_connection;
function PearRowDataGateway(&$connection) {
$this->_connection =& $connection;
}
function insertString() {
return "";
}
function updateString() {
return "";
}
function deleteString() {
return "";
}
function insert() {
$this->executeQuery($this->insertString());
}
function update() {
$this->executeQuery($this->updateString());
}
function delete() {
$this->executeQuery($this->deleteString());
}
function &load(&$row) {
// ...
}
function executeQuery($sql) {
$this->_connection->query($sql);
if (DB::isError($this->_connection)) {
die($this->_connection->getMessage());
}
}
}
?>
This would make the PearUsageGateway implementation look like this:
PHP Code:
<?php
define('USAGE_TABLE', '`usage`');
define('USAGE_TABLE_ID', 'id');
define('USAGE_TABLE_NAME', 'name');
class PearUsageGateway extends PearRowDataGateway {
var $_id;
var $_name;
function PearUsageGateway(&$connection) {
parent::PearRowDataGateway($connection);
}
function setName($name) {
$this->_name = $name;
}
function getName() {
return $this->_name;
}
function setUsageId($id) {
$this->_id = $id;
}
function getUsageId() {
return $this->_id;
}
function insertString() {
return "INSERT INTO " . USAGE_TABLE . " (" . USAGE_TABLE_NAME . ") VALUES ('" . $this->_name . "')";
}
function updateString() {
return "UPDATE " . USAGE_TABLE . " SET " . USAGE_TABLE_NAME . "='" . $this->_name . "' WHERE " . USAGE_TABLE_ID . "=" . $this->_id;
}
function deleteString() {
return "DELETE FROM " . USAGE_TABLE . " WHERE " . USAGE_TABLE_NAME . "='" . $this->_name . "'";
}
function &load(&$row) {
$gateway =& new PearUsageGateway($this->_connection);
$gateway->setUsageId($row['id']);
$gateway->setName($row['name']);
return $gateway;
}
}
?>
Unfortunately, this doesn't buy us much. Maybe if we can somehow make the "load" function more generic it would be worthwhile.
JT
Bookmarks