Generic Class Wrapper - 2

The code in the previous post was converted to use Zend Framework’s Db component for database access. One of the first things I noticed was that the code got smaller.

There are many ways to use Zend_Db, for this implementation, this approach fits well. Note the use of Zend_Config_Ini as well.

object.class.php changed, and item.class.php was converted to store the properties in an object, rather than an array. classes/db.class.php was deleted.

classes/object.class.php

<?php

require_once 'Zend/Config/Ini.php';
require_once 'Zend/Db.php';

Class Object
{
        private $db;

        public function __construct()
        {
                $Config = new Zend_Config_Ini('ini.php','database');
                $this->db = Zend_Db::factory($Config->database);
        }

        protected function load($sTable,$sIdName,$sId)
        {
                $this->db->setFetchMode(Zend_Db::FETCH_OBJ);
                $oResult = $this->db->fetchRow('SELECT * FROM `'.$sTable.'` WHERE `'.$sIdName.'` = ?', $this->db->quote($sId));
                return $oResult;
        }

        protected function save($sTable,$sIdName,$oArgs)
        {
                $sId=$oArgs->{$sIdName};
                unset($oArgs->{$sIdName});
                $aArgs=get_object_vars($oArgs);
                foreach ($aArgs as $k => $v)
                        $aArgs[$k]=$this->db->quote($v);
                if ($this->load($sTable,$sIdName,$sId))
                        $this->db->update($sTable,$aArgs,$sIdName.'='.$this->db->quote($sId));
                else
                        $this->db->insert($sTable,$aArgs);
                return $this->load($sTable,$sIdName,$sId);
        }

        protected function remove($sTable,$sIdName,$sId)
        {
                return $this->db->delete($sTable,$sIdName.'='.$db->quote($sId));
        }

        public function __destruct()
        {
                $this->db->closeConnection();
        }
}
?>

ini.php

<?php /*
[database]
database.adapter=pdo_mysql
database.params.host=localhost
database.params.dbname=dbname
database.params.username=username
database.params.password=password
*/ ?>