【问题标题】:Zend Session Identity losing object parametersZend Session Identity 丢失对象参数
【发布时间】:2011-04-29 16:11:52
【问题描述】:

我有一个奇怪的问题,我似乎无法追踪。 我有一个自定义类(“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']
        } 

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

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

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

echo $user->fullName; // Will print "John Smith" or whatever it is

但是,当我调用另一个操作并调用 Auth Identity 时,我会丢失存储在“_cdata”数组中的所有内容:

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

有什么想法吗?

【问题讨论】:

    标签: php zend-framework session zend-auth


    【解决方案1】:

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

    这使我们更深入地了解Zend_Db_Table_Row_Abstract 类的__sleep 方法,这是在$user 对象被序列化后被调用的方法。

    public function __sleep()
    {
        return array('_tableClass', '_primary', '_data', '_cleanData', '_readOnly' ,'_modifiedFields');
    }
    

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

    【讨论】:

    • 谢谢!我自己不会到那里的。
    • 是的,我清楚地记得被同样的问题困扰了好几个小时。编码愉快!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多