私は100以上のテーブルを持っています。フォームで提供されるユーザー名に応じて、これらの各テーブルにアクセスする必要があります。ユーザー名はテーブル名です。テーブルを表すクラスごとに 100 個のクラスを作成したくありません。これを行う方法はありますか?
1 に答える
0
ベースマッパークラスでこれに似たこのコードを使用します。
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;
}
}
このようなものを使用すると、Zend_Db_Table_Abstract (DbTable モデル) のインスタンスまたはテーブルの名前である文字列を渡すことができます。
//controller code
$username = $form->getValue('username');
$mapper = My_Mapper(NULL, $username);
$result = $mapper->fetchAll();
これで本当に終わったとは思いませんが、道を示すのに役立つはずです。
マッパーを使用しておらず、DbTable モデルのみを使用したい場合は、試してみてください (これは、Zend_Db_Table_Abstract を受け入れるマッパーに渡すことができます)。
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
function __construct(array $config) {
parent::__construct($config);
}
}
これをコントローラー/アクションで使用するには:
/**
* 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
または、コントローラー/アクションで Zend_Db_Table をそのまま使用することもできます。
$username = $form->getValue('username');
$db = new Zend_Db_Table($username);
$result = $db->fetchAll();
幸運を...
于 2012-06-23T11:29:47.907 に答える