Symfony2:如何使用自定义PdoSessionStorage将会话属性另存为DB上的自定义字段? [英] Symfony2: How to save a session attribute as a custom field on DB, with a custom PdoSessionStorage?
问题描述
我有一个扩展PdoSessionStorage的自定义类,但是我不知道如何从会话中捕获属性以将它们另存为数据库中的独立字段.
I have a custom class extending PdoSessionStorage, but I don't know how to catch attributes from the session to save them as independent fields in the database.
其他可能性是在我的自定义PdoSessionStorage类的sessionWrite($ if,$ data)方法中反序列化会话数据.但是我不知道如何反序列化$ data字符串以仅获取我想要的数据.
Other posibility is to unserialize session data in sessionWrite($if, $data) method of my custom PdoSessionStorage Class. But I don't know how to unserialize the $data string to get only data I want to.
我尝试过:
unserialize($data);
这会引发以下错误:
致命错误:未捕获的异常'ErrorException'和消息'通知:unserialize()[function.unserialize]:/myserver/myapp/src/app/myBundle/myCustomPdoSessionStorage.php第220行中的偏移量为82字节的错误在/myserver/myapp/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php:65中的堆栈跟踪:#0 [内部函数]:Symfony \ Component \ HttpKernel \ Debug \ ErrorHandler-> handle(8, 'unserialize()[...','/Applications/M ...',220,数组)#1/myserver/myapp/src/app/myBundle/myCustomPdoSessionStorage.php(220):unserialize('_ symfony2 | a :3:{...')#2 [内部功能]:app/myBundle/myCustomPdoSessionStorage-> sessionWrite('72b823b39d316dd ...','_symfony2 | a:3:{...')#3 {main}在第65行的/myserver/myapp/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php中抛出
Fatal error: Uncaught exception 'ErrorException' with message 'Notice: unserialize() [function.unserialize]: Error at offset 0 of 82 bytes in /myserver/myapp/src/app/myBundle/myCustomPdoSessionStorage.php line 220' in /myserver/myapp/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php:65 Stack trace: #0 [internal function]: Symfony\Component\HttpKernel\Debug\ErrorHandler->handle(8, 'unserialize() [...', '/Applications/M...', 220, Array) #1 /myserver/myapp/src/app/myBundle/myCustomPdoSessionStorage.php(220): unserialize('_symfony2|a:3:{...') #2 [internal function]: app/myBundle/myCustomPdoSessionStorage->sessionWrite('72b823b39d316dd...', '_symfony2|a:3:{...') #3 {main} thrown in /myserver/myapp/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php on line 65
有人可以帮我吗?
推荐答案
我已经找到了!
我的CustomPdoSessionStorage
类扩展了NativeSessionStorage
,它管理$_SESSION
数组(读,写等).
My CustomPdoSessionStorage
Class extends the NativeSessionStorage
, which manages the $_SESSION
array (read, write, etc...).
因此,在我的课堂上,我可以使用$this->read('userId')
并将其存储在数据库中.
So, from my class I can use $this->read('userId')
and store it on my DB.
非常感谢@hakre
So thanks a lot @hakre
这篇关于Symfony2:如何使用自定义PdoSessionStorage将会话属性另存为DB上的自定义字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!