I have over 100 tables. I need to access each of these tables depending on a username that is provide on a form. The username is the table name. I do not not want to create 100 of classes for each one representing a table. Is there a way i can do this?
Asked
Active
Viewed 396 times
1 Answers
0
I use this code similar to this in my base mapper class:
protected $_tableGateway = NULL;
protected $_tableName = NULL;
public function __construct(Zend_Db_Table_Abstract $tableGateway = NULL, $tableName = NULL) {
//set the tablename in a concrete mapper, set it here in constructor or create mutator to setTableName.
if (!is_null($tableName)) {
$this->_tableName = $tableName;
}
if (is_null($tableGateway)) {
$this->_tableGateway = new Zend_Db_Table($this->_tableName);
} else {
$this->_tableGateway = $tableGateway;
}
}
Something like this should allow you to pass in an instance of Zend_Db_Table_Abstract (DbTable model) or a string that is the name of the table.
//controller code
$username = $form->getValue('username');
$mapper = My_Mapper(NULL, $username);
$result = $mapper->fetchAll();
I don't think this is really finished, but should help show the way.
If you are not using mappers and just want to use DbTable Models you might try (this could be passed to a mapper that accepts Zend_Db_Table_Abstract):
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
function __construct(array $config) {
parent::__construct($config);
}
}
and to use this in your controller/action:
/**
* Supported params for $config are:
* - db = user-supplied instance of database connector,
* or key name of registry instance.
* - name = table name.
* - primary = string or array of primary key(s).
* - rowClass = row class name.
* - rowsetClass = rowset class name.
* - referenceMap = array structure to declare relationship
* to parent tables.
* - dependentTables = array of child tables.
* - metadataCache = cache for information from adapter describeTable().
*/
$username = $form->getValue('username');
$config = array('name'=> $username, 'primary' => 'id');
$model = new Application_Model_DbTable_Users($config);//This should setup your table
$result = $model->fetchAll(); //perform queries or execute methods defined in DbTable model
or you could just use Zend_Db_Table naked in your controller/action:
$username = $form->getValue('username');
$db = new Zend_Db_Table($username);
$result = $db->fetchAll();
Good Luck...

RockyFord
- 8,529
- 1
- 15
- 21
-
what do you mean by base mapper class? What would i have in the dbtable, in model, and mapper? – shorif2000 Jun 25 '12 at 10:42
-
a class you can extend to make other mapper classes. – RockyFord Jun 26 '12 at 04:05