2012-06-22 4 views
0

나는 100 개가 넘는 테이블을 가지고 있습니다. 양식에 제공되는 사용자 이름에 따라 각 테이블에 액세스해야합니다. 사용자 이름은 테이블 이름입니다. 나는 테이블을 나타내는 각각에 대해 100 개의 클래스를 생성하고 싶지 않습니다. 내가 할 수있는 방법이 있니?동적으로 클래스를 생성하거나 하나의 컨트롤러와 모델을 사용하여 여러 테이블에 액세스합니다.

답변

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(); 

저는 이것이 실제로 끝났다고 생각하지 않지만 길을 보여 주어야합니다.

당신이 매퍼를 사용하여 당신이 (이 Zend_Db_Table_Abstract을 받아들이는 매퍼로 전달 될 수있다) 시도 할 수 있습니다 DBTABLE 모델을 사용하지 않으면 :

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(); 

행운을 빌어 요 ...

+0

기본 매퍼 클래스 란 무엇입니까? dbtable, 모델 및 매퍼에서 무엇을 가질 수 있습니까? – shorif2000

+0

다른 매퍼 클래스를 만들기 위해 확장 할 수있는 클래스. – RockyFord

관련 문제