我无法访问从fetchRow()返回的数组
控制器:
$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;
}
数据库模型:
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($ user-> 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 ( ) )
最佳答案
首先,在您的控制器中返回模型实例,而不是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。
关于php - Zend Framework访问数据库表对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5837288/