Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.5 entities with generated proxies are unable to serialize due to a closure #236

Open
michaelsadlon opened this issue Nov 14, 2017 · 2 comments

Comments

@michaelsadlon
Copy link

I was working on upgrading to 5.5 and ran into some issues with the serialization. To be exact I upgraded analogue/orm from 5.4.0 to 5.5.18.

Basically in 5.5 the EntityProxy was replaced with a new ProxyFactory and when creating the generated class there is a closure inside. PHP is unable to serialize closures and fails with an exception:

Serialization of 'Closure' is not allowed

I ran into this when passing in my User entity to Notification::send(...) or when calling $user->notify(...). My User entity is still using the 5.4 base class and implements Notifiable. The serialization issue happened when the Queue tried to serialize the job (which contains the notifiable user).

I tried to work around this by implementing the Serializable interface and calling $this->toArray() and such but was not able to cover all the cases. Also upon a fresh boot Analogue ORM would throw an exception due to not being able to serialize() the dummy entity. Removing my overrides restored the original behavior.

I hacked a workaround by manually unsetting any proxied entites or collections but this isn't maintainable. I can probably separate User from Notifiable and create another class to deal with this but it all worked in 5.4 without issue.

My questions are:

  • Is it intended for the new proxy behavior to not serialize?
  • Any workarounds or fixes possible here?
  • If no workarounds or fixes: do you know what is the correct way to deal with serialization issues like this?

I would greatly appreciate any insight into this problem. Maybe I'm just doing something wrong but since this all worked in 5.4 it feels like a regression.

@michaelsadlon michaelsadlon changed the title 5.5 entites with generated proxies are unable to serialize due to a closure 5.5 entities with generated proxies are unable to serialize due to a closure Nov 14, 2017
@RemiCollin
Copy link
Member

@michaelsadlon thanks for reporting this.

The proxy library we implemented in 5.5 doesn't support serialization indeed : see Ocramius/ProxyManager#342

I guess we should use the same strategy Eloquent uses to deal with this issue (Eloquent object are not serializable either and uses a 'SerializesModels' trait to convert model to its primary key and fetch it from the database on wakeup).

@michaelsadlon
Copy link
Author

Thanks for the reply!

I noticed the SerializesModels trait and was wondering if that was also the case for Eloquent. Good to have confirmation. The only other case where I need to serialize a model is as a property on a job or notification but I believe SerializesModels already handles this.

I've put my 5.5 upgrade on hold for the moment but if I come back to it I'll try the strategy you mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants