-
Notifications
You must be signed in to change notification settings - Fork 62
[Question] Insufficient data for unserializing #64
Comments
It certainly sounds like the db is silently truncating the value. What column type? Size? Which database? Accessed over the network or via socket (if the former and MySQL, check max_allowed_packet). Anything unusual about the property names in your entity that the db encoding isn't going to handle? |
Database is PostgreSQL 9.4. Session data column: and got this: :6:{s:36:"\000User\Entity\UserEntity\000userId";N What are those zeros? According to google it's NULL char but where they come from? Most probably they are a problem here. <?php
namespace User\Entity;
class UserEntity
{
/**
* @var int
*/
private $userId;
Edit: I did some test and use json_encode/decode in |
Ok. After some more reading everything is clear now.
I did some test on pure PDO object with PDO::PARAM_LOB and bytea field, serializing/unserializing my entity and full object was stored and returned. My problem was due to incorrect behaviour of Zend\Session\SaveHandler\DbTableGateway which always converts data to string and it is not possible to store it as PARAM_LOB. Also, I don't know how to pass PARAM_LOB to Zend\Db\TableGateway. Possible solution: convert data to json (many db engines have appropriate fields) or use bin2hex/pack and store as BLOB. It can not be just serialize/unserialize because it will throw PDOException on bytea field and on text field, it will cut string on first null byte. |
I'd never noticed the NULLs around private property names either. But then I tend to avoid serialising objects in session. Personally I would just save the userId rather than the whole object. It'll be easy enough to fetch the entity back from the DB. |
Of course there are many workarounds. For now, I have just extended DbTableGateway to work on JSON data. I had some bad experiences with objects and ZF1 Zend_Session, but because I'm rewriting my entire app I wanted to do it right. Fetching back user entity with every request doesn't make sense. More natural would be writing data to the array and hydrate when needed. Still, it's a bug in Db save handler but also in the documentation that suggests the use of a text field. |
This repository has been closed and moved to laminas/laminas-session; a new issue has been opened at laminas/laminas-session#11. |
Hello, maybe someone will be able to help me. I'm trying to save UserEntity (simple getters and setters, 1:1 mapping to db table, no extra methods or logic) into SessionArrayStorage, save handler is db table (session value column - text without length restrictions).
When I just simply put stdClass from getResultRowObject of Zend Authentication into storage, everything works fine. But when I hydrate stdClass into UserEntity, data inside DB value column is truncated:
and warnings are generated:
PHP Warning: Insufficient data for unserializing
,Warning: session_start(): Failed to decode session object. Session has been destroyed
I've disabled DB storage and serialized data inside sess_xxx file was complete, getIdentity returned full UserEntity object. So, are there some restrictions when using DB storage and automatic serialization or is it my fault somewhere?
The text was updated successfully, but these errors were encountered: