douershuang7356 2011-04-29 20:52
浏览 39
已采纳

Zend Framework Access数据库表对象

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 ( ) )
  • 写回答

1条回答 默认 最新

  • doucuoyan0426 2011-04-29 21:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化