4
public function loginAction() {

   $auth = Zend_Auth::getInstance();    

   $DB = Zend_Db_Table_Abstract::getDefaultAdapter();
   $request=$this->getRequest();        

   $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
   $authAdapter->setTableName('user')
               ->setIdentityColumn('uname')
               ->setCredentialColumn('password');  

   $uname = $request->getParam('uname');
   $pass = $request->getParam('password');

   $authAdapter->setIdentity($uname);
   $authAdapter->setCredential($pass);

   $result = $authAdapter->authenticate($authAdapter);
   $this->view->assign('auth',$result);
   if($result->isValid()) {

       $data = $authAdapter->getResultRowObject(null,'pass');
       $auth->getStorage()->write($data);

       //$this->view->assign('dbdata',$data);

       $userInfo = new Zend_Session_Namespace('userInfo');
       $userInfo->userType = 'admin';

       //$this->_redirect('/admin/adminhome');
   } else {

       $this->_redirect('/admin/index?fail=true');
   }    

   $data = $authAdapter->getResultRowObject(null,'pass');

这里$data将返回一个像这样的对象:

stdClass Object ( [id] => 12 [uname] => user [password] => user [type] => user )

我想将其转换为数组。只是尝试toArray()了功能,但徒劳无功。我的要求是像这样检查用户类型:

if($data['type']=='admin'){
do something
} else {
do something 
}

谢谢

4

2 回答 2

7

要回答您的问题,请使用:

$data = $authAdapter->getResultRowObject(null,'pass');
$anArray = (array) $data;

或者更简单地说:

$data = (array) $authAdapter->getResultRowObject(null,'pass');

但是您不需要转换为数组。直接使用对象即可:

$data = $authAdapter->getResultRowObject(null,'pass');
if($data->type=='admin'){
do something
} else{
do something 
}
于 2012-07-19T04:48:50.677 回答
0

您可以使用get_object_vars()

$array = get_object_vars($data);

对于多维数组,您需要将其包装在 functon 中:

function toArray($data) {
  if (is_object($data)) {
    $data = get_object_vars($data);
  }
  if (is_array($data)) {
    return array_map(__FUNCTION__, $data);
  } else {
    return $d;
  }
}
于 2013-07-18T15:11:17.810 回答