Zend会话标识丢失对象参数

Nik*_*mov 0 php session zend-framework zend-auth

我有一个奇怪的问题,我似乎无法追查.我有一个自定义类("Person"),它扩展了代表用户的Zend_Db_Table_Row_Abstract.除此之外,此类还具有在init()方法中设置的自定义变量,例如:

class Person extends Zend_Db_Table_Row_Abstract
{
        protected $_cdata = array(); // non-db-table data gets put here through __set()

        public function init()
        {
           $this->fullName = $this->firstName." ".$this->lastName; // this is saved to $this->_cdata['fullName']
        } 
Run Code Online (Sandbox Code Playgroud)

登录后,我将此类的对象存储为Zend Auth Identity:

$r = $auth->authenticate($authAdapter);
if($r->isValid())
{
  $user = $db->getUserByEmail($email); // Retrieves an object of class "Person"
  $auth->getStorage()->write($user);
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我在与登录相同的操作请求中调用Auth Identity,它将正常工作:

echo $user->fullName; // Will print "John Smith" or whatever it is
Run Code Online (Sandbox Code Playgroud)

但是,当我调用另一个动作并调用Auth Identity时,我丢失了存储在"_cdata"数组中的任何内容:

$auth = Zend_Auth::getInstance();
if($auth->hasIdentity() {
   $user = $auth->getIdentity();
   echo $user->fullName; // Prints nothing...$_cdata['fullName'] does not exist.
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

小智 5

发生这种情况的原因是因为Zend_Auth身份数据在请求之间被序列化(和反序列化).

这引导我们仔细研究类的__sleep方法Zend_Db_Table_Row_Abstract,这是在$user序列化对象后调用的方法.

public function __sleep()
{
    return array('_tableClass', '_primary', '_data', '_cleanData', '_readOnly' ,'_modifiedFields');
}
Run Code Online (Sandbox Code Playgroud)

您需要做的是在Person类中重写此方法,以便它也包含$_cdata数组.然后,此属性将被序列化并在下一个HTTP请求中可用.