开发者

Zend Framework Access DB Table Object

I am having trouble accessing my array that is returned from fetchRow()

Controller:

$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 Model:

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

When I do print_r($user->id); I do not get the user id for the user. These are my results when I print the $user object in the controller.

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


First, in your controller you return model instance instead of getUser execution result. Should be this way:

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

       $users = new Application_Model_DbTable_Users();

        return $users->getUserId($username);

}

Then, in your model - getUser returns array of rows instead of row (there is fetchAll in your example). Try this way:

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;    
}

Btw, your function is called getUserId, but it seems you're trying to get user as a result. So, the function name should be getUser instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜