Laravel-mongodb: Serialization of 'MongoDB\BSON\UTCDateTime' is not allowed

Created on 10 Feb 2016  路  12Comments  路  Source: jenssegers/laravel-mongodb

This is when im trying to store the results into a redis cache

return $this->returnAsRoles(\Cache::tags(Role::CACHE_TAG)->rememberForever('non_global_roles', function () {
return Role::where('global_role', 0)->get()->toArray();
}));

Most helpful comment

Thanks all of you for your time and effort. I have updated PHP MongoDB extension from 1.1.9 to 1.2.2 and fixed the bug.

All 12 comments

Doing this will convert it from being an object to just a date string though
protected $dates = [
'updated_at',
'created_at'
];

and it happen to the MongoDB\BSON\ObjectID too

Hi all, I have the same issue. Actually, this is not the problem of jenssegers/laravel-mongodb package. The root cause is inside MongoDB driver changes of their implementation.
Still waiting for their fix. However, I have figured out another way to pass by this issue. The general idea is: you need to implement your own serialize/deserialize yourself for your model.

My solution: I create a custom Eloquent model which extend from Jenssegers\Mongodb\Eloquent\Model. Then, implements the \Serializable interface myself. This is the gist which introduces my approach: gist

In the above gist, I also try to detect whether the string is a 'serialised string' or not. The approach to detect this is implemented using exactly the same code with the 'is_serialized' method of Wordpress here

I hope this one is useful for everyone. I will refactor the gist to put into laravel mongodb package and make a pull request to fix this issue if the MongoDB team takes so long to fix this issue.

Nice temp patch Khoatran. Having same issue currently i have solved it by not caching the _id at all and setting
protected $dates = [
'updated_at',
'created_at'
];

hope they fix this at the monogdb lib level soon.

Fixed but not released yet as stable. However we can build from master manualy, worked like a charm. Reference: https://github.com/mongodb/mongo-php-library/issues/150

FYI: ext-mongodb 1.2.0alpha1 has been released with the necessary BSON serialization features.

Awesome news!

hey guys,

I'm also getting the same error:

ErrorException in SerializableClosure.php line 117:
Serialization of closure failed: Serialization of 'MongoDB\BSON\UTCDateTime' is not allowed

But I'm getting this with another package.

Please have a look to this:
https://github.com/jrean/laravel-user-verification/issues/94

Not sure, what is the exact issue is and with which package?

Any help would be appreciated.

Thanks,
Parth vora

@xparthx have you read @khoatran message? It's all explained there. You can either wait for the new extension that will suport it, build your alpha extension or make a work arround with the code to convert Bson/OjectID and BSON/UTCDateTime to some plain string and during unserialization convert them again to each of their types.

@ebisbe: I replied in https://github.com/jrean/laravel-user-verification/issues/94#issuecomment-266818520, but the stable releases supporting serialization of BSON classes are already available. I assume @xparthx is still using 1.1.x.

@jmikola thanks! I didn't know that the new connector was already available.

Thanks all of you for your time and effort. I have updated PHP MongoDB extension from 1.1.9 to 1.2.2 and fixed the bug.

Was this page helpful?
0 / 5 - 0 ratings