Zend Session Identity 丢失对象参数

发布于 2024-11-04 10:12:41 字数 1164 浏览 4 评论 0原文

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

有什么想法吗?

I have a strange problem I can't seem to track down.
I have a custom class ("Person") extending Zend_Db_Table_Row_Abstract that represents a user.
Among other things, this class has custom variables that are set in the init() method, for instance:

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']
        } 

Upon login, I store an object of this class as Zend Auth Identity:

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

Now, if I call Auth Identity in the same action request as the login, it will work alright:

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

However, when I call another action, and call Auth Identity, I lose whatever I have stored in "_cdata" array:

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2024-11-11 10:12:41

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

这让我们仔细研究 Zend_Db_Table_Row_Abstract 类的 __sleep 方法,该方法是在 $user 对象被序列化后被调用的方法。

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

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

The reason why that's happening is because Zend_Auth identity data gets serialized (and deserialized) between requests.

Which leads us to a closer look onto __sleep method of Zend_Db_Table_Row_Abstract class, which is the one that gets called once $user object is serialized.

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

What you need to do is to override this method in your Person class, so that it includes $_cdata array as well. Then this property will be serialized and available in the next HTTP request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文