2011-04-29 145 views
0

我無法訪問我的數組是從fetchRow()返回Zend框架Access數據庫表對象

控制器:

$user = $this->getUserId(); 
print_r($user->id); // This is where I am not getting the correct value 
public function getUserId() { 
      $auth = Zend_Auth::getInstance(); 
       if ($auth->hasIdentity()) 
        $username = $auth->getIdentity()->username; 

      $users = new Application_Model_DbTable_Users(); 
      $users->getUserId($username); 

      return $users; 

     } 

DB型號:

public function getUserId($username) { 
    $dbAdapter = Zend_Db_Table::getDefaultAdapter(); 
    $username = $dbAdapter->quote($username); 
    $row = $this->fetchAll('username = ' . $username); 
    if($row == NULL){ 
     throw new Exception("Result is null for $username"); 
    } 
    if (!$row) { 
     throw new Exception("Could not find user $username"); 
    }   
    return $row->toArray();  
} 

當我做的print_r ($用戶> ID);我沒有得到用戶的用戶ID。這些是我在控制器中打印$ user對象時的結果。

Application_Model_DbTable_Users Object ([_name:protected] => users [_definition:protected] => [_definitionConfigName:protected] => [_db:protected] => Zend_Db_Adapter_Pdo_Mysql Object ([_pdoType:protected] => mysql [_numericDataTypes:protected] => Array ([0] => 0 [1] => 1 [2] => 2 [INT] => 0 [INTEGER] => 0 [MEDIUMINT] => 0 [SMALLINT] => 0 [TINYINT] => 0 [BIGINT] => 1 [SERIAL] => 1 [DEC] => 2 [DECIMAL] => 2 [DOUBLE] => 2 [DOUBLE PRECISION] => 2 [FIXED] => 2 [FLOAT] => 2) [_defaultStmtClass:protected] => Zend_Db_Statement_Pdo [_config:protected] => Array ([host] => ***** [username] => ***** [password] => ***** [dbname] => autotest [charset] => [persistent] => [options] => Array ([caseFolding] => 0 [autoQuoteIdentifiers] => 1 [fetchMode] => 2) [driver_options] => Array ()) [_fetchMode:protected] => 2 [_profiler:protected] => Zend_Db_Profiler Object ([_queryProfiles:protected] => Array () [_enabled:protected] => [_filterElapsedSecs:protected] => [_filterTypes:protected] =>) [_defaultProfilerClass:protected] => Zend_Db_Profiler [_connection:protected] => PDO Object () [_caseFolding:protected] => 0 [_autoQuoteIdentifiers:protected] => 1 [_allowSerialization:protected] => 1 [_autoReconnectOnUnserialize:protected] =>) [_schema:protected] => [_cols:protected] => Array ([0] => id [1] => username [2] => password [3] => salt [4] => role [5] => date_created) [_primary:protected] => Array ([1] => id) [_identity:protected] => 1 [_sequence:protected] => 1 [_metadata:protected] => Array ([id] => Array ([SCHEMA_NAME] => [TABLE_NAME] => users [COLUMN_NAME] => id [COLUMN_POSITION] => 1 [DATA_TYPE] => int [DEFAULT] => [NULLABLE] => [LENGTH] => [SCALE] => [PRECISION] => [UNSIGNED] => [PRIMARY] => 1 [PRIMARY_POSITION] => 1 [IDENTITY] => 1) [username] => Array ([SCHEMA_NAME] => [TABLE_NAME] => users [COLUMN_NAME] => username [COLUMN_POSITION] => 2 [DATA_TYPE] => varchar [DEFAULT] => [NULLABLE] => [LENGTH] => 50 [SCALE] => [PRECISION] => [UNSIGNED] => [PRIMARY] => [PRIMARY_POSITION] => [IDENTITY] =>) [password] => Array ([SCHEMA_NAME] => [TABLE_NAME] => users [COLUMN_NAME] => password [COLUMN_POSITION] => 3 [DATA_TYPE] => varchar [DEFAULT] => [NULLABLE] => [LENGTH] => 50 [SCALE] => [PRECISION] => [UNSIGNED] => [PRIMARY] => [PRIMARY_POSITION] => [IDENTITY] =>) [salt] => Array ([SCHEMA_NAME] => [TABLE_NAME] => users [COLUMN_NAME] => salt [COLUMN_POSITION] => 4 [DATA_TYPE] => varchar [DEFAULT] => [NULLABLE] => [LENGTH] => 50 [SCALE] => [PRECISION] => [UNSIGNED] => [PRIMARY] => [PRIMARY_POSITION] => [IDENTITY] =>) [role] => Array ([SCHEMA_NAME] => [TABLE_NAME] => users [COLUMN_NAME] => role [COLUMN_POSITION] => 5 [DATA_TYPE] => varchar [DEFAULT] => [NULLABLE] => [LENGTH] => 50 [SCALE] => [PRECISION] => [UNSIGNED] => [PRIMARY] => [PRIMARY_POSITION] => [IDENTITY] =>) [date_created] => Array ([SCHEMA_NAME] => [TABLE_NAME] => users [COLUMN_NAME] => date_created [COLUMN_POSITION] => 6 [DATA_TYPE] => datetime [DEFAULT] => [NULLABLE] => [LENGTH] => [SCALE] => [PRECISION] => [UNSIGNED] => [PRIMARY] => [PRIMARY_POSITION] => [IDENTITY] =>)) [_metadataCache:protected] => [_metadataCacheInClass:protected] => 1 [_rowClass:protected] => Zend_Db_Table_Row [_rowsetClass:protected] => Zend_Db_Table_Rowset [_referenceMap:protected] => Array () [_dependentTables:protected] => Array () [_defaultSource:protected] => defaultNone [_defaultValues:protected] => Array ()) 

回答

2

首先,在您的控制器中,您返回模型實例而不是getUser執行結果。應該是這樣的:

public function getUserId() { 
     $auth = Zend_Auth::getInstance(); 
      if ($auth->hasIdentity()) 
       $username = $auth->getIdentity()->username; 

     $users = new Application_Model_DbTable_Users(); 

     return $users->getUserId($username); 

} 

然後,在你的模型 - 的getUser返回行而不是行的陣列(有使用fetchall在你的例子)。試試這個方法:

public function getUserId($username) { 
    $dbAdapter = Zend_Db_Table::getDefaultAdapter(); 
    $username = $dbAdapter->quote($username); 
    $row = $this->fetchRow('username = ' . $username); 
    if(!$row){ 
    throw new Exception("Result is null for $username"); 
    } 

    return $row;  
} 

順便說一句,你的函數被調用getUserId,但似乎你正在試圖獲得用戶的結果。所以,函數名應該是getUser。