php - Zend Framework Access DB Table Object -


i having trouble accessing array returned fetchrow()

controller:

$user = $this->getuserid(); print_r($user->id); // not getting 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 null $username");     }     if (!$row) {         throw new exception("could not find user $username");     }             return $row->toarray();     } 

when print_r($user->id); not user id user. these results when print $user object in 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 controller return model instance instead of getuser execution result. should 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 model - getuser returns array of rows instead of row (there fetchall in example). try 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 null $username");     }      return $row;     } 

btw, function called getuserid, seems you're trying user result. so, function name should getuser instead.


Comments