On 31 Oct 2008, at 15:37, Giovanni A. D. wrote:

Hello,
I'm following the Rob Allen's "Getting started with zend framework"
tutorial (http://akrabat.com/zend-framework-tutorial/) and coming to
the point of setting up the model I was wondering how I could take a
table prefix from my .ini config file and use it..

Here is my solution:

class Albums extends Zend_Db_Table {
        protected $_name = '';

        public function __construct() {
                $this->_name =_TABLE_PFX.'albums';
                parent::__construct();
        }

}


I've set _TABLE_PFX in the bootstrap.php file reading it from the
config (the .ini file):
define('_TABLE_PFX', $config->db->table_pfx);

Can you tell me is this solution can be considered "ok" or if there is
a better way to achieve this?


Hi,

I would probably extend Zend_Db_Table_Abstract and automatically add the prefix when setting up the table name. Something like this:


class App_Db_Table_Abstract extends Zend_Db_Table_Abstract
{
    protected function _setupTableName()
    {
        parent::_setupTableName();

        $config = Zend_Registry('config');
        $prefix = $config->db->table_pfx;
        $this->_name = $prefix . '_' . $this->_name;
    }
}


Then in the Albums class, you can use carry on as normal and the prefix is "auto-magically" prepended:

class Albums extends App_Db_Table_Abstract
{
        $this->_name = 'albums';
}



Regards,

Rob...

Reply via email to