Creating Table Prefix Using Zend Framework

Are you little confused configuring the table prefix in zf? Still I don’t why, why there is no default configuration on zend framework to set up the table prefix, if you have an idea please share with us. Ok, well let’s start to configure it manually.

First add an addition line


//application/configs/application.ini

; Database connection
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = password
resources.db.params.dbname = database
resources.db.params.prefix = my_
resources.db.isDefaultTableAdapter = true

Usually I named it prefix obvious, wasn’t it? :) i.e. resources.db.params.prefix =

It is not yet over, you should need to set it up in your bootstrap


// application/bootstrap.php

protected function _initResgisterConstant()
{
// Get the value of application.ini
$configArray  = $this->getOptions();
$this->config = new Zend_Config($configArray);
$config       = $this->config;

Zend_Registry::set('dbConfig', $config->resources->db);

// Register the table prefix
$tablePrefix 	= $config->resources->db->params->prefix;
Zend_Registry::set('tablePrefix', $tablePrefix);
}

Then the final step is to set it in you model file, here’s an example


// application/models/Tables/Albums.php

protected $_name = 'albums';

public function __construct() {
// Construct the table prefix
$this->_name = Zend_Registry::get('tablePrefix') . $this->_name;
parent::__construct();
}

This post is tagged: , , ,


4 Responses to “Creating Table Prefix Using Zend Framework”
  1. 07.23.2010

    That’s good idea, but sadly too many configs… :(

  2. 01.26.2011

    I must know where you got your layout from im trying to start my own blog and yours is the coolest.

  3. Blog is good!:) Perhaps you learn me blog myself??


Leave a Reply