Zend Session Identity 丢失对象参数
我有一个奇怪的问题,我似乎无法追踪。 我有一个自定义类(“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况的原因是
Zend_Auth
身份数据在请求之间被序列化(和反序列化)。这让我们仔细研究
Zend_Db_Table_Row_Abstract
类的__sleep
方法,该方法是在$user
对象被序列化后被调用的方法。您需要做的是在
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 ofZend_Db_Table_Row_Abstract
class, which is the one that gets called once$user
object is serialized.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.